{
 "cells": [
  {
   "cell_type": "code",
   "id": "7bf18d2b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T05:29:19.493728Z",
     "start_time": "2025-11-18T05:29:19.484033Z"
    }
   },
   "source": [
    "import numpy as np # 数据处理最重要的模块\n",
    "import pandas as pd # 数据处理最重要的模块\n",
    "import scipy.stats as stats # 统计模块\n",
    "import scipy\n",
    "# import pymysql  # 导入数据库模块\n",
    "\n",
    "from datetime import datetime # 时间模块\n",
    "import statsmodels.formula.api as smf  # OLS regression\n",
    "\n",
    "# import pyreadr # read RDS file\n",
    "\n",
    "from matplotlib import style\n",
    "import matplotlib.pyplot as plt  # 画图模块\n",
    "import matplotlib.dates as mdates\n",
    "\n",
    "from matplotlib.font_manager import FontProperties # 作图中文\n",
    "from pylab import mpl\n",
    "#mpl.rcParams['font.sans-serif'] = ['SimHei']\n",
    "#plt.rcParams['font.family'] = 'Times New Roman'\n",
    "\n",
    "#输出矢量图 渲染矢量图\n",
    "%matplotlib inline\n",
    "%config InlineBackend.figure_format = 'svg'\n",
    "\n",
    "from IPython.core.interactiveshell import InteractiveShell # jupyter运行输出的模块\n",
    "#显示每一个运行结果\n",
    "InteractiveShell.ast_node_interactivity = 'all'\n",
    "\n",
    "#设置行不限制数量\n",
    "#pd.set_option('display.max_rows',None)\n",
    "\n",
    "#设置列不限制数量\n",
    "pd.set_option('display.max_columns', None)"
   ],
   "outputs": [],
   "execution_count": 35
  },
  {
   "cell_type": "code",
   "id": "41e88908",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T05:29:24.483135Z",
     "start_time": "2025-11-18T05:29:21.481749Z"
    }
   },
   "source": [
    "cross = pd.read_csv('C://Users//10203//Desktop//AAA//TRD_Mnth202509.csv')\n",
    "from pandas.tseries.offsets import MonthEnd\n",
    "# 处理月份格式\n",
    "cross['month'] = pd.to_datetime(cross['Trdmnt'], format='%Y-%m') + MonthEnd(1)\n",
    "# 补齐股票代码 如果不满6位 在前面补上0\n",
    "cross['Stkcd'] = cross['Stkcd'].apply(lambda x: '{:0>6}'.format(x)) # 6位股票代码\n",
    "# 重命名列以匹配后续代码\n",
    "cross.rename(columns={'Mretwd': 'Return', 'Msmvosd': 'floatingvalue', 'Msmvttl': 'totalvalue'}, inplace=True)\n",
    "\n",
    "# 导入无风险利率数据\n",
    "rf_data = pd.read_csv('C://Users//10203//Desktop//AAA//Marketret_mon_stock2024.csv')\n",
    "# 处理月份格式\n",
    "rf_data['month'] = pd.to_datetime(rf_data['month'], format='%b %Y') + MonthEnd(1)\n",
    "# 只保留需要的列\n",
    "rf_data = rf_data[['month', 'rfmonth']]\n",
    "\n",
    "# 合并无风险利率到cross\n",
    "cross = pd.merge(cross, rf_data, on='month', how='left')\n",
    "\n",
    "# 添加一个序列 用来统计公司上市的第几个月\n",
    "cross = cross.sort_values(by=['Stkcd', 'month'])\n",
    "cross['list_month'] = cross.groupby('Stkcd').cumcount() + 1\n",
    "\n",
    "# 计算超额收益率\n",
    "cross['ret'] = cross['Return'] - cross['rfmonth']\n",
    "cross['floatingvalue'] = cross['floatingvalue'] * 1000\n",
    "cross['totalvalue'] = cross['totalvalue'] * 1000\n",
    "\n",
    "cross"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         Stkcd   Trdmnt  Opndt  Mopnprc  Clsdt  Mclsprc  Mnshrtrd  \\\n",
       "0       000001  1991-04      3    49.00     30    43.68     13400   \n",
       "1       000001  1991-05      2    43.46     31    38.34    187800   \n",
       "2       000001  1991-06      1    38.53     28    33.99     30600   \n",
       "3       000001  1991-07      1    33.65     31    29.54      6100   \n",
       "4       000001  1991-08      1    29.39     31    15.00   3243100   \n",
       "...        ...      ...    ...      ...    ...      ...       ...   \n",
       "886908  920992  2025-05      6    20.00     30    20.46  92609592   \n",
       "886909  920992  2025-06      3    20.40     30    21.26  73152031   \n",
       "886910  920992  2025-07      1    21.13     31    22.44  96064221   \n",
       "886911  920992  2025-08      1    22.12     29    21.65  63953451   \n",
       "886912  920992  2025-09      1    21.75     30    21.07  83403669   \n",
       "\n",
       "            Mnvaltrd  floatingvalue    totalvalue  Ndaytrd    Return  \\\n",
       "0       6.150000e+05   1.157520e+09  2.118487e+09       20       NaN   \n",
       "1       7.675000e+06   1.016010e+09  1.859497e+09       24 -0.122253   \n",
       "2       1.094000e+06   9.007350e+08  1.648521e+09       23 -0.113459   \n",
       "3       1.940430e+05   7.828100e+08  1.432695e+09       16 -0.130921   \n",
       "4       4.957624e+07   6.748338e+08  1.346275e+09       15 -0.411588   \n",
       "...              ...            ...           ...      ...       ...   \n",
       "886908  1.922463e+09   5.707967e+08  1.979115e+09       19  0.051312   \n",
       "886909  1.580767e+09   5.931152e+08  2.056500e+09       20  0.039101   \n",
       "886910  2.126994e+09   6.260351e+08  2.170642e+09       23  0.055503   \n",
       "886911  1.432608e+09   6.039955e+08  2.094225e+09       21 -0.035205   \n",
       "886912  1.981091e+09   5.878146e+08  2.038121e+09       22 -0.026790   \n",
       "\n",
       "          Mretnd  Markettype    Capchgdt  Ahshrtrd_M  Ahvaltrd_M      month  \\\n",
       "0            NaN           4  1991-04-03         NaN         NaN 1991-04-30   \n",
       "1       0.235714           4  1991-04-03         NaN         NaN 1991-05-31   \n",
       "2      -0.113459           4  1991-04-03         NaN         NaN 1991-06-30   \n",
       "3      -0.130921           4  1991-04-03         NaN         NaN 1991-07-31   \n",
       "4      -0.411587           4  1991-08-01         NaN         NaN 1991-08-31   \n",
       "...          ...         ...         ...         ...         ...        ...   \n",
       "886908  0.051715          64  2023-04-25         0.0         0.0 2025-05-31   \n",
       "886909  0.039101          64  2023-04-25         0.0         0.0 2025-06-30   \n",
       "886910  0.055503          64  2023-04-25         0.0         0.0 2025-07-31   \n",
       "886911 -0.035205          64  2023-04-25         0.0         0.0 2025-08-31   \n",
       "886912 -0.026790          64  2023-04-25         0.0         0.0 2025-09-30   \n",
       "\n",
       "         rfmonth  list_month       ret  \n",
       "0       0.006651           1       NaN  \n",
       "1       0.006092           2 -0.128345  \n",
       "2       0.006092           3 -0.119551  \n",
       "3       0.006092           4 -0.137013  \n",
       "4       0.006092           5 -0.417680  \n",
       "...          ...         ...       ...  \n",
       "886908       NaN          32       NaN  \n",
       "886909       NaN          33       NaN  \n",
       "886910       NaN          34       NaN  \n",
       "886911       NaN          35       NaN  \n",
       "886912       NaN          36       NaN  \n",
       "\n",
       "[886913 rows x 21 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stkcd</th>\n",
       "      <th>Trdmnt</th>\n",
       "      <th>Opndt</th>\n",
       "      <th>Mopnprc</th>\n",
       "      <th>Clsdt</th>\n",
       "      <th>Mclsprc</th>\n",
       "      <th>Mnshrtrd</th>\n",
       "      <th>Mnvaltrd</th>\n",
       "      <th>floatingvalue</th>\n",
       "      <th>totalvalue</th>\n",
       "      <th>Ndaytrd</th>\n",
       "      <th>Return</th>\n",
       "      <th>Mretnd</th>\n",
       "      <th>Markettype</th>\n",
       "      <th>Capchgdt</th>\n",
       "      <th>Ahshrtrd_M</th>\n",
       "      <th>Ahvaltrd_M</th>\n",
       "      <th>month</th>\n",
       "      <th>rfmonth</th>\n",
       "      <th>list_month</th>\n",
       "      <th>ret</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001</td>\n",
       "      <td>1991-04</td>\n",
       "      <td>3</td>\n",
       "      <td>49.00</td>\n",
       "      <td>30</td>\n",
       "      <td>43.68</td>\n",
       "      <td>13400</td>\n",
       "      <td>6.150000e+05</td>\n",
       "      <td>1.157520e+09</td>\n",
       "      <td>2.118487e+09</td>\n",
       "      <td>20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4</td>\n",
       "      <td>1991-04-03</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1991-04-30</td>\n",
       "      <td>0.006651</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000001</td>\n",
       "      <td>1991-05</td>\n",
       "      <td>2</td>\n",
       "      <td>43.46</td>\n",
       "      <td>31</td>\n",
       "      <td>38.34</td>\n",
       "      <td>187800</td>\n",
       "      <td>7.675000e+06</td>\n",
       "      <td>1.016010e+09</td>\n",
       "      <td>1.859497e+09</td>\n",
       "      <td>24</td>\n",
       "      <td>-0.122253</td>\n",
       "      <td>0.235714</td>\n",
       "      <td>4</td>\n",
       "      <td>1991-04-03</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1991-05-31</td>\n",
       "      <td>0.006092</td>\n",
       "      <td>2</td>\n",
       "      <td>-0.128345</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000001</td>\n",
       "      <td>1991-06</td>\n",
       "      <td>1</td>\n",
       "      <td>38.53</td>\n",
       "      <td>28</td>\n",
       "      <td>33.99</td>\n",
       "      <td>30600</td>\n",
       "      <td>1.094000e+06</td>\n",
       "      <td>9.007350e+08</td>\n",
       "      <td>1.648521e+09</td>\n",
       "      <td>23</td>\n",
       "      <td>-0.113459</td>\n",
       "      <td>-0.113459</td>\n",
       "      <td>4</td>\n",
       "      <td>1991-04-03</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1991-06-30</td>\n",
       "      <td>0.006092</td>\n",
       "      <td>3</td>\n",
       "      <td>-0.119551</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000001</td>\n",
       "      <td>1991-07</td>\n",
       "      <td>1</td>\n",
       "      <td>33.65</td>\n",
       "      <td>31</td>\n",
       "      <td>29.54</td>\n",
       "      <td>6100</td>\n",
       "      <td>1.940430e+05</td>\n",
       "      <td>7.828100e+08</td>\n",
       "      <td>1.432695e+09</td>\n",
       "      <td>16</td>\n",
       "      <td>-0.130921</td>\n",
       "      <td>-0.130921</td>\n",
       "      <td>4</td>\n",
       "      <td>1991-04-03</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1991-07-31</td>\n",
       "      <td>0.006092</td>\n",
       "      <td>4</td>\n",
       "      <td>-0.137013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000001</td>\n",
       "      <td>1991-08</td>\n",
       "      <td>1</td>\n",
       "      <td>29.39</td>\n",
       "      <td>31</td>\n",
       "      <td>15.00</td>\n",
       "      <td>3243100</td>\n",
       "      <td>4.957624e+07</td>\n",
       "      <td>6.748338e+08</td>\n",
       "      <td>1.346275e+09</td>\n",
       "      <td>15</td>\n",
       "      <td>-0.411588</td>\n",
       "      <td>-0.411587</td>\n",
       "      <td>4</td>\n",
       "      <td>1991-08-01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1991-08-31</td>\n",
       "      <td>0.006092</td>\n",
       "      <td>5</td>\n",
       "      <td>-0.417680</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>886908</th>\n",
       "      <td>920992</td>\n",
       "      <td>2025-05</td>\n",
       "      <td>6</td>\n",
       "      <td>20.00</td>\n",
       "      <td>30</td>\n",
       "      <td>20.46</td>\n",
       "      <td>92609592</td>\n",
       "      <td>1.922463e+09</td>\n",
       "      <td>5.707967e+08</td>\n",
       "      <td>1.979115e+09</td>\n",
       "      <td>19</td>\n",
       "      <td>0.051312</td>\n",
       "      <td>0.051715</td>\n",
       "      <td>64</td>\n",
       "      <td>2023-04-25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2025-05-31</td>\n",
       "      <td>NaN</td>\n",
       "      <td>32</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>886909</th>\n",
       "      <td>920992</td>\n",
       "      <td>2025-06</td>\n",
       "      <td>3</td>\n",
       "      <td>20.40</td>\n",
       "      <td>30</td>\n",
       "      <td>21.26</td>\n",
       "      <td>73152031</td>\n",
       "      <td>1.580767e+09</td>\n",
       "      <td>5.931152e+08</td>\n",
       "      <td>2.056500e+09</td>\n",
       "      <td>20</td>\n",
       "      <td>0.039101</td>\n",
       "      <td>0.039101</td>\n",
       "      <td>64</td>\n",
       "      <td>2023-04-25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2025-06-30</td>\n",
       "      <td>NaN</td>\n",
       "      <td>33</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>886910</th>\n",
       "      <td>920992</td>\n",
       "      <td>2025-07</td>\n",
       "      <td>1</td>\n",
       "      <td>21.13</td>\n",
       "      <td>31</td>\n",
       "      <td>22.44</td>\n",
       "      <td>96064221</td>\n",
       "      <td>2.126994e+09</td>\n",
       "      <td>6.260351e+08</td>\n",
       "      <td>2.170642e+09</td>\n",
       "      <td>23</td>\n",
       "      <td>0.055503</td>\n",
       "      <td>0.055503</td>\n",
       "      <td>64</td>\n",
       "      <td>2023-04-25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2025-07-31</td>\n",
       "      <td>NaN</td>\n",
       "      <td>34</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>886911</th>\n",
       "      <td>920992</td>\n",
       "      <td>2025-08</td>\n",
       "      <td>1</td>\n",
       "      <td>22.12</td>\n",
       "      <td>29</td>\n",
       "      <td>21.65</td>\n",
       "      <td>63953451</td>\n",
       "      <td>1.432608e+09</td>\n",
       "      <td>6.039955e+08</td>\n",
       "      <td>2.094225e+09</td>\n",
       "      <td>21</td>\n",
       "      <td>-0.035205</td>\n",
       "      <td>-0.035205</td>\n",
       "      <td>64</td>\n",
       "      <td>2023-04-25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2025-08-31</td>\n",
       "      <td>NaN</td>\n",
       "      <td>35</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>886912</th>\n",
       "      <td>920992</td>\n",
       "      <td>2025-09</td>\n",
       "      <td>1</td>\n",
       "      <td>21.75</td>\n",
       "      <td>30</td>\n",
       "      <td>21.07</td>\n",
       "      <td>83403669</td>\n",
       "      <td>1.981091e+09</td>\n",
       "      <td>5.878146e+08</td>\n",
       "      <td>2.038121e+09</td>\n",
       "      <td>22</td>\n",
       "      <td>-0.026790</td>\n",
       "      <td>-0.026790</td>\n",
       "      <td>64</td>\n",
       "      <td>2023-04-25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2025-09-30</td>\n",
       "      <td>NaN</td>\n",
       "      <td>36</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>886913 rows × 21 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 36
  },
  {
   "cell_type": "code",
   "id": "7b97058c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T05:29:33.275384Z",
     "start_time": "2025-11-18T05:29:26.607874Z"
    }
   },
   "source": [
    "# 生成下一个月的收益率\n",
    "# 创建所有日期和股票代码的完整组合\n",
    "all_months = pd.DataFrame(cross['month'].unique(), columns=['month'])\n",
    "all_stocks = pd.DataFrame(cross['Stkcd'].unique(), columns=['Stkcd'])\n",
    "# 创建笛卡尔积 - 所有股票和所有月份的组合\n",
    "full_index = all_stocks.merge(all_months, how='cross')\n",
    "\n",
    "# 将原始数据合并到完整索引中\n",
    "cross_full = full_index.merge(cross, on=['Stkcd', 'month'], how='left')\n",
    "# 按股票代码和月份排序\n",
    "cross_full = cross_full.sort_values(['Stkcd', 'month'])\n",
    "# 使用shift生成下月收益率\n",
    "cross_full['next_ret'] = cross_full.groupby('Stkcd')['ret'].shift(-1)\n",
    "\n",
    "# 只保留原始数据存在的行\n",
    "cross = cross.merge(cross_full[['Stkcd', 'month', 'next_ret']], \n",
    "                    on=['Stkcd', 'month'], how='right')\n",
    "\n",
    "# 添加一个变量 用来统计过去一年的交易日数量之和\n",
    "cross['Cumsum_tradingday'] = cross.groupby('Stkcd')['Ndaytrd'].transform(lambda x: x.rolling(window=12, min_periods=1).sum())\n",
    "print(\"方法1完成：补全日期序列后使用shift\")\n",
    "\n",
    "cross = cross[(cross['month'] >= '1995-01-31') & (cross['month'] <= '2024-12-31')]\n",
    "cross"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方法1完成：补全日期序列后使用shift\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "          Stkcd   Trdmnt  Opndt  Mopnprc  Clsdt  Mclsprc     Mnshrtrd  \\\n",
       "49       000001  1995-01    3.0    10.50   27.0    10.54   17488100.0   \n",
       "50       000001  1995-02   13.0    10.60   28.0    10.63    8544200.0   \n",
       "51       000001  1995-03    1.0    10.63   31.0    10.76   18810600.0   \n",
       "52       000001  1995-04    3.0    10.78   28.0     9.69   13235500.0   \n",
       "53       000001  1995-05    2.0     9.69   31.0     9.80   53197900.0   \n",
       "...         ...      ...    ...      ...    ...      ...          ...   \n",
       "2453228  920992  2024-08    1.0     8.50   30.0     8.19   14436632.0   \n",
       "2453229  920992  2024-09    2.0     8.15   30.0    10.46   18109819.0   \n",
       "2453230  920992  2024-10    8.0    13.02   31.0    14.55   86774237.0   \n",
       "2453231  920992  2024-11    1.0    14.69   29.0    18.08  130783392.0   \n",
       "2453232  920992  2024-12    2.0    18.41   31.0    13.69   87260377.0   \n",
       "\n",
       "             Mnvaltrd  floatingvalue    totalvalue  Ndaytrd    Return  \\\n",
       "49       1.867690e+08   3.134297e+09  4.543463e+09     19.0  0.005725   \n",
       "50       9.192790e+07   3.161060e+09  4.582260e+09     12.0  0.008539   \n",
       "51       2.059094e+08   3.199719e+09  4.638299e+09     23.0  0.012230   \n",
       "52       1.380604e+08   2.881531e+09  4.177055e+09     20.0 -0.099442   \n",
       "53       6.049730e+08   2.914242e+09  4.224473e+09     22.0  0.011352   \n",
       "...               ...            ...           ...      ...       ...   \n",
       "2453228  1.185912e+08   2.284861e+08  7.922264e+08     22.0 -0.037603   \n",
       "2453229  1.566140e+08   2.918149e+08  1.011806e+09     19.0  0.277167   \n",
       "2453230  1.145494e+09   4.059185e+08  1.407435e+09     18.0  0.391013   \n",
       "2453231  2.346534e+09   5.043990e+08  1.748895e+09     21.0  0.242612   \n",
       "2453232  1.426585e+09   3.819260e+08  1.324246e+09     22.0 -0.242810   \n",
       "\n",
       "           Mretnd  Markettype    Capchgdt  Ahshrtrd_M  Ahvaltrd_M      month  \\\n",
       "49       0.005725         4.0  1994-09-02         NaN         NaN 1995-01-31   \n",
       "50       0.008539         4.0  1994-09-02         NaN         NaN 1995-02-28   \n",
       "51       0.012230         4.0  1994-09-02         NaN         NaN 1995-03-31   \n",
       "52      -0.099442         4.0  1994-09-02         NaN         NaN 1995-04-30   \n",
       "53       0.011352         4.0  1994-09-02         NaN         NaN 1995-05-31   \n",
       "...           ...         ...         ...         ...         ...        ...   \n",
       "2453228 -0.037603        64.0  2023-04-25         0.0         0.0 2024-08-31   \n",
       "2453229  0.277167        64.0  2023-04-25         0.0         0.0 2024-09-30   \n",
       "2453230  0.391013        64.0  2023-04-25         0.0         0.0 2024-10-31   \n",
       "2453231  0.242612        64.0  2023-04-25         0.0         0.0 2024-11-30   \n",
       "2453232 -0.242810        64.0  2023-04-25         0.0         0.0 2024-12-31   \n",
       "\n",
       "          rfmonth  list_month       ret  next_ret  Cumsum_tradingday  \n",
       "49       0.008719        46.0 -0.002994 -0.000180              247.0  \n",
       "50       0.008719        47.0 -0.000180  0.003511              244.0  \n",
       "51       0.008719        48.0  0.003511 -0.108161              244.0  \n",
       "52       0.008719        49.0 -0.108161  0.002633              243.0  \n",
       "53       0.008719        50.0  0.002633 -0.073005              244.0  \n",
       "...           ...         ...       ...       ...                ...  \n",
       "2453228  0.001118        23.0 -0.038721  0.276049              242.0  \n",
       "2453229  0.001118        24.0  0.276049  0.389895              241.0  \n",
       "2453230  0.001118        25.0  0.389895  0.241494              242.0  \n",
       "2453231  0.001118        26.0  0.241494 -0.243928              241.0  \n",
       "2453232  0.001118        27.0 -0.243928       NaN              242.0  \n",
       "\n",
       "[2112840 rows x 23 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stkcd</th>\n",
       "      <th>Trdmnt</th>\n",
       "      <th>Opndt</th>\n",
       "      <th>Mopnprc</th>\n",
       "      <th>Clsdt</th>\n",
       "      <th>Mclsprc</th>\n",
       "      <th>Mnshrtrd</th>\n",
       "      <th>Mnvaltrd</th>\n",
       "      <th>floatingvalue</th>\n",
       "      <th>totalvalue</th>\n",
       "      <th>Ndaytrd</th>\n",
       "      <th>Return</th>\n",
       "      <th>Mretnd</th>\n",
       "      <th>Markettype</th>\n",
       "      <th>Capchgdt</th>\n",
       "      <th>Ahshrtrd_M</th>\n",
       "      <th>Ahvaltrd_M</th>\n",
       "      <th>month</th>\n",
       "      <th>rfmonth</th>\n",
       "      <th>list_month</th>\n",
       "      <th>ret</th>\n",
       "      <th>next_ret</th>\n",
       "      <th>Cumsum_tradingday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>000001</td>\n",
       "      <td>1995-01</td>\n",
       "      <td>3.0</td>\n",
       "      <td>10.50</td>\n",
       "      <td>27.0</td>\n",
       "      <td>10.54</td>\n",
       "      <td>17488100.0</td>\n",
       "      <td>1.867690e+08</td>\n",
       "      <td>3.134297e+09</td>\n",
       "      <td>4.543463e+09</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0.005725</td>\n",
       "      <td>0.005725</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1994-09-02</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1995-01-31</td>\n",
       "      <td>0.008719</td>\n",
       "      <td>46.0</td>\n",
       "      <td>-0.002994</td>\n",
       "      <td>-0.000180</td>\n",
       "      <td>247.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50</th>\n",
       "      <td>000001</td>\n",
       "      <td>1995-02</td>\n",
       "      <td>13.0</td>\n",
       "      <td>10.60</td>\n",
       "      <td>28.0</td>\n",
       "      <td>10.63</td>\n",
       "      <td>8544200.0</td>\n",
       "      <td>9.192790e+07</td>\n",
       "      <td>3.161060e+09</td>\n",
       "      <td>4.582260e+09</td>\n",
       "      <td>12.0</td>\n",
       "      <td>0.008539</td>\n",
       "      <td>0.008539</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1994-09-02</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1995-02-28</td>\n",
       "      <td>0.008719</td>\n",
       "      <td>47.0</td>\n",
       "      <td>-0.000180</td>\n",
       "      <td>0.003511</td>\n",
       "      <td>244.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>000001</td>\n",
       "      <td>1995-03</td>\n",
       "      <td>1.0</td>\n",
       "      <td>10.63</td>\n",
       "      <td>31.0</td>\n",
       "      <td>10.76</td>\n",
       "      <td>18810600.0</td>\n",
       "      <td>2.059094e+08</td>\n",
       "      <td>3.199719e+09</td>\n",
       "      <td>4.638299e+09</td>\n",
       "      <td>23.0</td>\n",
       "      <td>0.012230</td>\n",
       "      <td>0.012230</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1994-09-02</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1995-03-31</td>\n",
       "      <td>0.008719</td>\n",
       "      <td>48.0</td>\n",
       "      <td>0.003511</td>\n",
       "      <td>-0.108161</td>\n",
       "      <td>244.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>52</th>\n",
       "      <td>000001</td>\n",
       "      <td>1995-04</td>\n",
       "      <td>3.0</td>\n",
       "      <td>10.78</td>\n",
       "      <td>28.0</td>\n",
       "      <td>9.69</td>\n",
       "      <td>13235500.0</td>\n",
       "      <td>1.380604e+08</td>\n",
       "      <td>2.881531e+09</td>\n",
       "      <td>4.177055e+09</td>\n",
       "      <td>20.0</td>\n",
       "      <td>-0.099442</td>\n",
       "      <td>-0.099442</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1994-09-02</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1995-04-30</td>\n",
       "      <td>0.008719</td>\n",
       "      <td>49.0</td>\n",
       "      <td>-0.108161</td>\n",
       "      <td>0.002633</td>\n",
       "      <td>243.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53</th>\n",
       "      <td>000001</td>\n",
       "      <td>1995-05</td>\n",
       "      <td>2.0</td>\n",
       "      <td>9.69</td>\n",
       "      <td>31.0</td>\n",
       "      <td>9.80</td>\n",
       "      <td>53197900.0</td>\n",
       "      <td>6.049730e+08</td>\n",
       "      <td>2.914242e+09</td>\n",
       "      <td>4.224473e+09</td>\n",
       "      <td>22.0</td>\n",
       "      <td>0.011352</td>\n",
       "      <td>0.011352</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1994-09-02</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1995-05-31</td>\n",
       "      <td>0.008719</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.002633</td>\n",
       "      <td>-0.073005</td>\n",
       "      <td>244.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2453228</th>\n",
       "      <td>920992</td>\n",
       "      <td>2024-08</td>\n",
       "      <td>1.0</td>\n",
       "      <td>8.50</td>\n",
       "      <td>30.0</td>\n",
       "      <td>8.19</td>\n",
       "      <td>14436632.0</td>\n",
       "      <td>1.185912e+08</td>\n",
       "      <td>2.284861e+08</td>\n",
       "      <td>7.922264e+08</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.037603</td>\n",
       "      <td>-0.037603</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2023-04-25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-08-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>23.0</td>\n",
       "      <td>-0.038721</td>\n",
       "      <td>0.276049</td>\n",
       "      <td>242.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2453229</th>\n",
       "      <td>920992</td>\n",
       "      <td>2024-09</td>\n",
       "      <td>2.0</td>\n",
       "      <td>8.15</td>\n",
       "      <td>30.0</td>\n",
       "      <td>10.46</td>\n",
       "      <td>18109819.0</td>\n",
       "      <td>1.566140e+08</td>\n",
       "      <td>2.918149e+08</td>\n",
       "      <td>1.011806e+09</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0.277167</td>\n",
       "      <td>0.277167</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2023-04-25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-09-30</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>24.0</td>\n",
       "      <td>0.276049</td>\n",
       "      <td>0.389895</td>\n",
       "      <td>241.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2453230</th>\n",
       "      <td>920992</td>\n",
       "      <td>2024-10</td>\n",
       "      <td>8.0</td>\n",
       "      <td>13.02</td>\n",
       "      <td>31.0</td>\n",
       "      <td>14.55</td>\n",
       "      <td>86774237.0</td>\n",
       "      <td>1.145494e+09</td>\n",
       "      <td>4.059185e+08</td>\n",
       "      <td>1.407435e+09</td>\n",
       "      <td>18.0</td>\n",
       "      <td>0.391013</td>\n",
       "      <td>0.391013</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2023-04-25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-10-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>25.0</td>\n",
       "      <td>0.389895</td>\n",
       "      <td>0.241494</td>\n",
       "      <td>242.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2453231</th>\n",
       "      <td>920992</td>\n",
       "      <td>2024-11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>14.69</td>\n",
       "      <td>29.0</td>\n",
       "      <td>18.08</td>\n",
       "      <td>130783392.0</td>\n",
       "      <td>2.346534e+09</td>\n",
       "      <td>5.043990e+08</td>\n",
       "      <td>1.748895e+09</td>\n",
       "      <td>21.0</td>\n",
       "      <td>0.242612</td>\n",
       "      <td>0.242612</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2023-04-25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-11-30</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0.241494</td>\n",
       "      <td>-0.243928</td>\n",
       "      <td>241.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2453232</th>\n",
       "      <td>920992</td>\n",
       "      <td>2024-12</td>\n",
       "      <td>2.0</td>\n",
       "      <td>18.41</td>\n",
       "      <td>31.0</td>\n",
       "      <td>13.69</td>\n",
       "      <td>87260377.0</td>\n",
       "      <td>1.426585e+09</td>\n",
       "      <td>3.819260e+08</td>\n",
       "      <td>1.324246e+09</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.242810</td>\n",
       "      <td>-0.242810</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2023-04-25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>27.0</td>\n",
       "      <td>-0.243928</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2112840 rows × 23 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 37
  },
  {
   "cell_type": "code",
   "id": "d785e229",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T05:29:36.282967Z",
     "start_time": "2025-11-18T05:29:36.262756Z"
    }
   },
   "source": [
    "from pandas.tseries.offsets import MonthEnd # 月末\n",
    "Market_ret = pd.read_csv('C://Users//10203//Desktop//AAA//Marketret_mon_stock2024.csv')\n",
    "Market_ret['month'] = pd.to_datetime(Market_ret['month'], format='%b %Y') + MonthEnd(0)\n",
    "Market_ret.set_index('month', inplace=True)\n",
    "Market_ret.sort_index(inplace=True)\n",
    "Market_ret = Market_ret.drop(columns=['Unnamed: 0'])\n",
    "Market_ret.rename(columns={'ret': 'MKT'}, inplace=True)\n",
    "Market_ret"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             MarketR  MarketR_e   rfmonth       MKT     ret_e  marketret3  \\\n",
       "month                                                                       \n",
       "1991-01-31  0.029998   0.036554  0.006930  0.023068  0.029624   -0.084127   \n",
       "1991-02-28  0.010203   0.021860  0.006930  0.003273  0.014930   -0.183573   \n",
       "1991-03-31 -0.099663  -0.060425  0.006930 -0.106593 -0.067355   -0.252928   \n",
       "1991-04-30 -0.079652  -0.031449  0.006651 -0.086303 -0.038100   -0.234776   \n",
       "1991-05-31 -0.074521   0.005375  0.006092 -0.080613 -0.000717   -0.236294   \n",
       "...              ...        ...       ...       ...       ...         ...   \n",
       "2024-08-31 -0.031468  -0.033685  0.001118 -0.032586 -0.034803    0.151050   \n",
       "2024-09-30  0.195992   0.227078  0.001118  0.194874  0.225960    0.200147   \n",
       "2024-10-31 -0.003409   0.070220  0.001118 -0.004527  0.069102    0.000228   \n",
       "2024-11-30  0.009724   0.045312  0.001118  0.008606  0.044194         NaN   \n",
       "2024-12-31 -0.002679  -0.040799  0.001118 -0.003797 -0.041917         NaN   \n",
       "\n",
       "            marketret6  marketret12        Q  \n",
       "month                                         \n",
       "1991-01-31   -0.305662     0.254049  1991 Q1  \n",
       "1991-02-28   -0.384745     0.241492  1991 Q1  \n",
       "1991-03-31   -0.445049     0.288857  1991 Q1  \n",
       "1991-04-30   -0.394937     0.691749  1991 Q2  \n",
       "1991-05-31    0.181673     1.542701  1991 Q2  \n",
       "...                ...          ...      ...  \n",
       "2024-08-31         NaN          NaN  2024 Q3  \n",
       "2024-09-30         NaN          NaN  2024 Q3  \n",
       "2024-10-31         NaN          NaN  2024 Q4  \n",
       "2024-11-30         NaN          NaN  2024 Q4  \n",
       "2024-12-31         NaN          NaN  2024 Q4  \n",
       "\n",
       "[408 rows x 9 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MarketR</th>\n",
       "      <th>MarketR_e</th>\n",
       "      <th>rfmonth</th>\n",
       "      <th>MKT</th>\n",
       "      <th>ret_e</th>\n",
       "      <th>marketret3</th>\n",
       "      <th>marketret6</th>\n",
       "      <th>marketret12</th>\n",
       "      <th>Q</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>month</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1991-01-31</th>\n",
       "      <td>0.029998</td>\n",
       "      <td>0.036554</td>\n",
       "      <td>0.006930</td>\n",
       "      <td>0.023068</td>\n",
       "      <td>0.029624</td>\n",
       "      <td>-0.084127</td>\n",
       "      <td>-0.305662</td>\n",
       "      <td>0.254049</td>\n",
       "      <td>1991 Q1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1991-02-28</th>\n",
       "      <td>0.010203</td>\n",
       "      <td>0.021860</td>\n",
       "      <td>0.006930</td>\n",
       "      <td>0.003273</td>\n",
       "      <td>0.014930</td>\n",
       "      <td>-0.183573</td>\n",
       "      <td>-0.384745</td>\n",
       "      <td>0.241492</td>\n",
       "      <td>1991 Q1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1991-03-31</th>\n",
       "      <td>-0.099663</td>\n",
       "      <td>-0.060425</td>\n",
       "      <td>0.006930</td>\n",
       "      <td>-0.106593</td>\n",
       "      <td>-0.067355</td>\n",
       "      <td>-0.252928</td>\n",
       "      <td>-0.445049</td>\n",
       "      <td>0.288857</td>\n",
       "      <td>1991 Q1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1991-04-30</th>\n",
       "      <td>-0.079652</td>\n",
       "      <td>-0.031449</td>\n",
       "      <td>0.006651</td>\n",
       "      <td>-0.086303</td>\n",
       "      <td>-0.038100</td>\n",
       "      <td>-0.234776</td>\n",
       "      <td>-0.394937</td>\n",
       "      <td>0.691749</td>\n",
       "      <td>1991 Q2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1991-05-31</th>\n",
       "      <td>-0.074521</td>\n",
       "      <td>0.005375</td>\n",
       "      <td>0.006092</td>\n",
       "      <td>-0.080613</td>\n",
       "      <td>-0.000717</td>\n",
       "      <td>-0.236294</td>\n",
       "      <td>0.181673</td>\n",
       "      <td>1.542701</td>\n",
       "      <td>1991 Q2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-08-31</th>\n",
       "      <td>-0.031468</td>\n",
       "      <td>-0.033685</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>-0.032586</td>\n",
       "      <td>-0.034803</td>\n",
       "      <td>0.151050</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2024 Q3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-30</th>\n",
       "      <td>0.195992</td>\n",
       "      <td>0.227078</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>0.194874</td>\n",
       "      <td>0.225960</td>\n",
       "      <td>0.200147</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2024 Q3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-10-31</th>\n",
       "      <td>-0.003409</td>\n",
       "      <td>0.070220</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>-0.004527</td>\n",
       "      <td>0.069102</td>\n",
       "      <td>0.000228</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2024 Q4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-30</th>\n",
       "      <td>0.009724</td>\n",
       "      <td>0.045312</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>0.008606</td>\n",
       "      <td>0.044194</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2024 Q4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>-0.002679</td>\n",
       "      <td>-0.040799</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>-0.003797</td>\n",
       "      <td>-0.041917</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2024 Q4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>408 rows × 9 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 38
  },
  {
   "cell_type": "code",
   "id": "5c04f76c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T05:29:38.935029Z",
     "start_time": "2025-11-18T05:29:38.301449Z"
    }
   },
   "source": [
    "cross = pd.merge(cross,Market_ret[['MKT']],left_on='month',right_on='month',how='left')\n",
    "cross"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          Stkcd   Trdmnt  Opndt  Mopnprc  Clsdt  Mclsprc     Mnshrtrd  \\\n",
       "0        000001  1995-01    3.0    10.50   27.0    10.54   17488100.0   \n",
       "1        000001  1995-02   13.0    10.60   28.0    10.63    8544200.0   \n",
       "2        000001  1995-03    1.0    10.63   31.0    10.76   18810600.0   \n",
       "3        000001  1995-04    3.0    10.78   28.0     9.69   13235500.0   \n",
       "4        000001  1995-05    2.0     9.69   31.0     9.80   53197900.0   \n",
       "...         ...      ...    ...      ...    ...      ...          ...   \n",
       "2112835  920992  2024-08    1.0     8.50   30.0     8.19   14436632.0   \n",
       "2112836  920992  2024-09    2.0     8.15   30.0    10.46   18109819.0   \n",
       "2112837  920992  2024-10    8.0    13.02   31.0    14.55   86774237.0   \n",
       "2112838  920992  2024-11    1.0    14.69   29.0    18.08  130783392.0   \n",
       "2112839  920992  2024-12    2.0    18.41   31.0    13.69   87260377.0   \n",
       "\n",
       "             Mnvaltrd  floatingvalue    totalvalue  Ndaytrd    Return  \\\n",
       "0        1.867690e+08   3.134297e+09  4.543463e+09     19.0  0.005725   \n",
       "1        9.192790e+07   3.161060e+09  4.582260e+09     12.0  0.008539   \n",
       "2        2.059094e+08   3.199719e+09  4.638299e+09     23.0  0.012230   \n",
       "3        1.380604e+08   2.881531e+09  4.177055e+09     20.0 -0.099442   \n",
       "4        6.049730e+08   2.914242e+09  4.224473e+09     22.0  0.011352   \n",
       "...               ...            ...           ...      ...       ...   \n",
       "2112835  1.185912e+08   2.284861e+08  7.922264e+08     22.0 -0.037603   \n",
       "2112836  1.566140e+08   2.918149e+08  1.011806e+09     19.0  0.277167   \n",
       "2112837  1.145494e+09   4.059185e+08  1.407435e+09     18.0  0.391013   \n",
       "2112838  2.346534e+09   5.043990e+08  1.748895e+09     21.0  0.242612   \n",
       "2112839  1.426585e+09   3.819260e+08  1.324246e+09     22.0 -0.242810   \n",
       "\n",
       "           Mretnd  Markettype    Capchgdt  Ahshrtrd_M  Ahvaltrd_M      month  \\\n",
       "0        0.005725         4.0  1994-09-02         NaN         NaN 1995-01-31   \n",
       "1        0.008539         4.0  1994-09-02         NaN         NaN 1995-02-28   \n",
       "2        0.012230         4.0  1994-09-02         NaN         NaN 1995-03-31   \n",
       "3       -0.099442         4.0  1994-09-02         NaN         NaN 1995-04-30   \n",
       "4        0.011352         4.0  1994-09-02         NaN         NaN 1995-05-31   \n",
       "...           ...         ...         ...         ...         ...        ...   \n",
       "2112835 -0.037603        64.0  2023-04-25         0.0         0.0 2024-08-31   \n",
       "2112836  0.277167        64.0  2023-04-25         0.0         0.0 2024-09-30   \n",
       "2112837  0.391013        64.0  2023-04-25         0.0         0.0 2024-10-31   \n",
       "2112838  0.242612        64.0  2023-04-25         0.0         0.0 2024-11-30   \n",
       "2112839 -0.242810        64.0  2023-04-25         0.0         0.0 2024-12-31   \n",
       "\n",
       "          rfmonth  list_month       ret  next_ret  Cumsum_tradingday       MKT  \n",
       "0        0.008719        46.0 -0.002994 -0.000180              247.0 -0.125017  \n",
       "1        0.008719        47.0 -0.000180  0.003511              244.0 -0.022778  \n",
       "2        0.008719        48.0  0.003511 -0.108161              244.0  0.122163  \n",
       "3        0.008719        49.0 -0.108161  0.002633              243.0 -0.113023  \n",
       "4        0.008719        50.0  0.002633 -0.073005              244.0  0.166736  \n",
       "...           ...         ...       ...       ...                ...       ...  \n",
       "2112835  0.001118        23.0 -0.038721  0.276049              242.0 -0.032586  \n",
       "2112836  0.001118        24.0  0.276049  0.389895              241.0  0.194874  \n",
       "2112837  0.001118        25.0  0.389895  0.241494              242.0 -0.004527  \n",
       "2112838  0.001118        26.0  0.241494 -0.243928              241.0  0.008606  \n",
       "2112839  0.001118        27.0 -0.243928       NaN              242.0 -0.003797  \n",
       "\n",
       "[2112840 rows x 24 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stkcd</th>\n",
       "      <th>Trdmnt</th>\n",
       "      <th>Opndt</th>\n",
       "      <th>Mopnprc</th>\n",
       "      <th>Clsdt</th>\n",
       "      <th>Mclsprc</th>\n",
       "      <th>Mnshrtrd</th>\n",
       "      <th>Mnvaltrd</th>\n",
       "      <th>floatingvalue</th>\n",
       "      <th>totalvalue</th>\n",
       "      <th>Ndaytrd</th>\n",
       "      <th>Return</th>\n",
       "      <th>Mretnd</th>\n",
       "      <th>Markettype</th>\n",
       "      <th>Capchgdt</th>\n",
       "      <th>Ahshrtrd_M</th>\n",
       "      <th>Ahvaltrd_M</th>\n",
       "      <th>month</th>\n",
       "      <th>rfmonth</th>\n",
       "      <th>list_month</th>\n",
       "      <th>ret</th>\n",
       "      <th>next_ret</th>\n",
       "      <th>Cumsum_tradingday</th>\n",
       "      <th>MKT</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001</td>\n",
       "      <td>1995-01</td>\n",
       "      <td>3.0</td>\n",
       "      <td>10.50</td>\n",
       "      <td>27.0</td>\n",
       "      <td>10.54</td>\n",
       "      <td>17488100.0</td>\n",
       "      <td>1.867690e+08</td>\n",
       "      <td>3.134297e+09</td>\n",
       "      <td>4.543463e+09</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0.005725</td>\n",
       "      <td>0.005725</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1994-09-02</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1995-01-31</td>\n",
       "      <td>0.008719</td>\n",
       "      <td>46.0</td>\n",
       "      <td>-0.002994</td>\n",
       "      <td>-0.000180</td>\n",
       "      <td>247.0</td>\n",
       "      <td>-0.125017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000001</td>\n",
       "      <td>1995-02</td>\n",
       "      <td>13.0</td>\n",
       "      <td>10.60</td>\n",
       "      <td>28.0</td>\n",
       "      <td>10.63</td>\n",
       "      <td>8544200.0</td>\n",
       "      <td>9.192790e+07</td>\n",
       "      <td>3.161060e+09</td>\n",
       "      <td>4.582260e+09</td>\n",
       "      <td>12.0</td>\n",
       "      <td>0.008539</td>\n",
       "      <td>0.008539</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1994-09-02</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1995-02-28</td>\n",
       "      <td>0.008719</td>\n",
       "      <td>47.0</td>\n",
       "      <td>-0.000180</td>\n",
       "      <td>0.003511</td>\n",
       "      <td>244.0</td>\n",
       "      <td>-0.022778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000001</td>\n",
       "      <td>1995-03</td>\n",
       "      <td>1.0</td>\n",
       "      <td>10.63</td>\n",
       "      <td>31.0</td>\n",
       "      <td>10.76</td>\n",
       "      <td>18810600.0</td>\n",
       "      <td>2.059094e+08</td>\n",
       "      <td>3.199719e+09</td>\n",
       "      <td>4.638299e+09</td>\n",
       "      <td>23.0</td>\n",
       "      <td>0.012230</td>\n",
       "      <td>0.012230</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1994-09-02</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1995-03-31</td>\n",
       "      <td>0.008719</td>\n",
       "      <td>48.0</td>\n",
       "      <td>0.003511</td>\n",
       "      <td>-0.108161</td>\n",
       "      <td>244.0</td>\n",
       "      <td>0.122163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000001</td>\n",
       "      <td>1995-04</td>\n",
       "      <td>3.0</td>\n",
       "      <td>10.78</td>\n",
       "      <td>28.0</td>\n",
       "      <td>9.69</td>\n",
       "      <td>13235500.0</td>\n",
       "      <td>1.380604e+08</td>\n",
       "      <td>2.881531e+09</td>\n",
       "      <td>4.177055e+09</td>\n",
       "      <td>20.0</td>\n",
       "      <td>-0.099442</td>\n",
       "      <td>-0.099442</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1994-09-02</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1995-04-30</td>\n",
       "      <td>0.008719</td>\n",
       "      <td>49.0</td>\n",
       "      <td>-0.108161</td>\n",
       "      <td>0.002633</td>\n",
       "      <td>243.0</td>\n",
       "      <td>-0.113023</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000001</td>\n",
       "      <td>1995-05</td>\n",
       "      <td>2.0</td>\n",
       "      <td>9.69</td>\n",
       "      <td>31.0</td>\n",
       "      <td>9.80</td>\n",
       "      <td>53197900.0</td>\n",
       "      <td>6.049730e+08</td>\n",
       "      <td>2.914242e+09</td>\n",
       "      <td>4.224473e+09</td>\n",
       "      <td>22.0</td>\n",
       "      <td>0.011352</td>\n",
       "      <td>0.011352</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1994-09-02</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1995-05-31</td>\n",
       "      <td>0.008719</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.002633</td>\n",
       "      <td>-0.073005</td>\n",
       "      <td>244.0</td>\n",
       "      <td>0.166736</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2112835</th>\n",
       "      <td>920992</td>\n",
       "      <td>2024-08</td>\n",
       "      <td>1.0</td>\n",
       "      <td>8.50</td>\n",
       "      <td>30.0</td>\n",
       "      <td>8.19</td>\n",
       "      <td>14436632.0</td>\n",
       "      <td>1.185912e+08</td>\n",
       "      <td>2.284861e+08</td>\n",
       "      <td>7.922264e+08</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.037603</td>\n",
       "      <td>-0.037603</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2023-04-25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-08-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>23.0</td>\n",
       "      <td>-0.038721</td>\n",
       "      <td>0.276049</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.032586</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2112836</th>\n",
       "      <td>920992</td>\n",
       "      <td>2024-09</td>\n",
       "      <td>2.0</td>\n",
       "      <td>8.15</td>\n",
       "      <td>30.0</td>\n",
       "      <td>10.46</td>\n",
       "      <td>18109819.0</td>\n",
       "      <td>1.566140e+08</td>\n",
       "      <td>2.918149e+08</td>\n",
       "      <td>1.011806e+09</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0.277167</td>\n",
       "      <td>0.277167</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2023-04-25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-09-30</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>24.0</td>\n",
       "      <td>0.276049</td>\n",
       "      <td>0.389895</td>\n",
       "      <td>241.0</td>\n",
       "      <td>0.194874</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2112837</th>\n",
       "      <td>920992</td>\n",
       "      <td>2024-10</td>\n",
       "      <td>8.0</td>\n",
       "      <td>13.02</td>\n",
       "      <td>31.0</td>\n",
       "      <td>14.55</td>\n",
       "      <td>86774237.0</td>\n",
       "      <td>1.145494e+09</td>\n",
       "      <td>4.059185e+08</td>\n",
       "      <td>1.407435e+09</td>\n",
       "      <td>18.0</td>\n",
       "      <td>0.391013</td>\n",
       "      <td>0.391013</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2023-04-25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-10-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>25.0</td>\n",
       "      <td>0.389895</td>\n",
       "      <td>0.241494</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.004527</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2112838</th>\n",
       "      <td>920992</td>\n",
       "      <td>2024-11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>14.69</td>\n",
       "      <td>29.0</td>\n",
       "      <td>18.08</td>\n",
       "      <td>130783392.0</td>\n",
       "      <td>2.346534e+09</td>\n",
       "      <td>5.043990e+08</td>\n",
       "      <td>1.748895e+09</td>\n",
       "      <td>21.0</td>\n",
       "      <td>0.242612</td>\n",
       "      <td>0.242612</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2023-04-25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-11-30</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0.241494</td>\n",
       "      <td>-0.243928</td>\n",
       "      <td>241.0</td>\n",
       "      <td>0.008606</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2112839</th>\n",
       "      <td>920992</td>\n",
       "      <td>2024-12</td>\n",
       "      <td>2.0</td>\n",
       "      <td>18.41</td>\n",
       "      <td>31.0</td>\n",
       "      <td>13.69</td>\n",
       "      <td>87260377.0</td>\n",
       "      <td>1.426585e+09</td>\n",
       "      <td>3.819260e+08</td>\n",
       "      <td>1.324246e+09</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.242810</td>\n",
       "      <td>-0.242810</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2023-04-25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>27.0</td>\n",
       "      <td>-0.243928</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.003797</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2112840 rows × 24 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 39
  },
  {
   "cell_type": "code",
   "id": "f7d216b2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:08:18.256411Z",
     "start_time": "2025-11-18T05:29:40.709212Z"
    }
   },
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "import statsmodels.api as sm\n",
    "\n",
    "# 定义一个函数来计算滚动回归的贝塔和使用的数据点数量\n",
    "def rolling_beta_per_stock(data, window_months=60):\n",
    "    # 计算最小需要的数据点数量（2/3的窗口期）\n",
    "    min_periods = max(1, int(np.ceil(window_months * 2 / 3)))\n",
    "    print(f\"Beta估计设置:\")\n",
    "    print(f\"  窗口期: {window_months} 个月\")\n",
    "    print(f\"  最少需要: {min_periods} 个月有数据\")\n",
    "    print(\"=\"*60)\n",
    "\n",
    "    betas = []\n",
    "    months = []\n",
    "    Stkcds = []\n",
    "    data_counts = []\n",
    "\n",
    "    # 按股票分组\n",
    "    grouped = data.groupby('Stkcd')\n",
    "\n",
    "    # 使用tqdm添加进度条\n",
    "    for Stkcd, group in tqdm(grouped, desc=\"计算Beta\", total=len(grouped)):\n",
    "        group = group.set_index('month').sort_index()\n",
    "        end_months = group.index.unique()\n",
    "\n",
    "        for end_month in end_months:\n",
    "            start_month = end_month - pd.DateOffset(months=window_months)\n",
    "            window_data = group.loc[start_month:end_month]\n",
    "\n",
    "            # 只有当数据点数量达到最小要求时才进行回归\n",
    "            if len(window_data) >= min_periods:\n",
    "                # 删除缺失值\n",
    "                window_data_clean = window_data.dropna(subset=['ret', 'MKT'])\n",
    "\n",
    "                # 再次检查清理后的数据量\n",
    "                if len(window_data_clean) >= min_periods:\n",
    "                    X = sm.add_constant(window_data_clean['MKT'])\n",
    "                    y = window_data_clean['ret']\n",
    "                    model = sm.OLS(y, X).fit()\n",
    "\n",
    "                    beta = model.params['MKT']\n",
    "                    betas.append(beta)\n",
    "                    months.append(end_month)\n",
    "                    Stkcds.append(Stkcd)\n",
    "                    data_counts.append(len(window_data_clean))\n",
    "\n",
    "    result_df = pd.DataFrame({'Stkcd': Stkcds, 'month': months, 'beta': betas, 'data_count': data_counts})\n",
    "\n",
    "    print(f\"\\n估计完成:\")\n",
    "    print(f\"  总观测数: {len(result_df)}\")\n",
    "    print(f\"  平均数据点数: {result_df['data_count'].mean():.1f}\")\n",
    "    print(f\"  数据点数统计:\")\n",
    "    print(result_df['data_count'].describe())\n",
    "\n",
    "    return result_df\n",
    "\n",
    "# 计算每只股票的滚动贝塔和数据点数量\n",
    "rolling_betas = rolling_beta_per_stock(cross)\n",
    "\n",
    "# 打印结果\n",
    "rolling_betas"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Beta估计设置:\n",
      "  窗口期: 60 个月\n",
      "  最少需要: 40 个月有数据\n",
      "============================================================\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "计算Beta: 100%|██████████| 5869/5869 [38:35<00:00,  2.53it/s] \n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "估计完成:\n",
      "  总观测数: 630276\n",
      "  平均数据点数: 58.1\n",
      "  数据点数统计:\n",
      "count    630276.000000\n",
      "mean         58.100191\n",
      "std           5.075211\n",
      "min          40.000000\n",
      "25%          57.000000\n",
      "50%          61.000000\n",
      "75%          61.000000\n",
      "max          61.000000\n",
      "Name: data_count, dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "         Stkcd      month      beta  data_count\n",
       "0       000001 1998-04-30  1.155400          40\n",
       "1       000001 1998-05-31  1.148875          41\n",
       "2       000001 1998-06-30  1.167744          42\n",
       "3       000001 1998-07-31  1.167735          43\n",
       "4       000001 1998-08-31  1.152495          44\n",
       "...        ...        ...       ...         ...\n",
       "630271  920964 2024-08-31  0.725409          49\n",
       "630272  920964 2024-09-30  0.983053          50\n",
       "630273  920964 2024-10-31  0.877199          51\n",
       "630274  920964 2024-11-30  0.860914          52\n",
       "630275  920964 2024-12-31  0.872943          53\n",
       "\n",
       "[630276 rows x 4 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stkcd</th>\n",
       "      <th>month</th>\n",
       "      <th>beta</th>\n",
       "      <th>data_count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-04-30</td>\n",
       "      <td>1.155400</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-05-31</td>\n",
       "      <td>1.148875</td>\n",
       "      <td>41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-06-30</td>\n",
       "      <td>1.167744</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-07-31</td>\n",
       "      <td>1.167735</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-08-31</td>\n",
       "      <td>1.152495</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>630271</th>\n",
       "      <td>920964</td>\n",
       "      <td>2024-08-31</td>\n",
       "      <td>0.725409</td>\n",
       "      <td>49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>630272</th>\n",
       "      <td>920964</td>\n",
       "      <td>2024-09-30</td>\n",
       "      <td>0.983053</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>630273</th>\n",
       "      <td>920964</td>\n",
       "      <td>2024-10-31</td>\n",
       "      <td>0.877199</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>630274</th>\n",
       "      <td>920964</td>\n",
       "      <td>2024-11-30</td>\n",
       "      <td>0.860914</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>630275</th>\n",
       "      <td>920964</td>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>0.872943</td>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>630276 rows × 4 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 40
  },
  {
   "cell_type": "code",
   "id": "6024b05c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:08:24.102244Z",
     "start_time": "2025-11-18T06:08:21.893987Z"
    }
   },
   "source": [
    "# save rolling_betas\n",
    "rolling_betas.to_csv('C://Users//10203//Desktop//AAA//rolling_betas.csv', index=False)"
   ],
   "outputs": [],
   "execution_count": 41
  },
  {
   "cell_type": "code",
   "id": "7ae3ae3b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:08:27.312626Z",
     "start_time": "2025-11-18T06:08:26.008026Z"
    }
   },
   "source": [
    "cross_beta = pd.merge(cross,rolling_betas,on=(\"Stkcd\",'month'),how='left')\n",
    "cross_beta = cross_beta.dropna(subset=['beta'])\n",
    "cross_beta"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          Stkcd   Trdmnt  Opndt  Mopnprc  Clsdt  Mclsprc     Mnshrtrd  \\\n",
       "39       000001  1998-04    1.0    21.50   30.0    20.86  135781919.0   \n",
       "40       000001  1998-05    4.0    20.90   29.0    20.07  108329870.0   \n",
       "41       000001  1998-06    1.0    20.10   30.0    17.12   91965412.0   \n",
       "42       000001  1998-07    1.0    17.58   31.0    17.26  102722557.0   \n",
       "43       000001  1998-08    3.0    17.28   31.0    16.14   71852835.0   \n",
       "...         ...      ...    ...      ...    ...      ...          ...   \n",
       "2109595  920964  2024-08    1.0     3.03   30.0     2.77   36090137.0   \n",
       "2109596  920964  2024-09    2.0     2.80   30.0     3.62   49005108.0   \n",
       "2109597  920964  2024-10    8.0     4.36   31.0    12.31  219750340.0   \n",
       "2109598  920964  2024-11    1.0    13.40   29.0     9.63  628989566.0   \n",
       "2109599  920964  2024-12    2.0     9.63   31.0     7.02  251277231.0   \n",
       "\n",
       "             Mnvaltrd  floatingvalue    totalvalue  Ndaytrd    Return  \\\n",
       "39       2.931748e+09   2.233903e+10  3.237153e+10     21.0 -0.032916   \n",
       "40       2.208166e+09   2.149301e+10  3.114557e+10     20.0 -0.037872   \n",
       "41       1.746011e+09   1.833385e+10  2.656762e+10     22.0 -0.146986   \n",
       "42       1.740172e+09   1.848378e+10  2.678488e+10     23.0  0.008178   \n",
       "43       1.172085e+09   1.728437e+10  2.504681e+10     21.0 -0.064890   \n",
       "...               ...            ...           ...      ...       ...   \n",
       "2109595  1.037190e+08   4.969253e+08  7.235462e+08     22.0 -0.082782   \n",
       "2109596  1.488490e+08   6.494114e+08  9.455730e+08     19.0  0.306858   \n",
       "2109597  1.478570e+09   2.208358e+09  3.215470e+09     14.0  2.400551   \n",
       "2109598  7.034681e+09   1.727578e+09  2.515433e+09     21.0 -0.217709   \n",
       "2109599  2.284998e+09   1.259356e+09  1.833680e+09     22.0 -0.271028   \n",
       "\n",
       "           Mretnd  Markettype    Capchgdt  Ahshrtrd_M  Ahvaltrd_M      month  \\\n",
       "39      -0.032916         4.0  1997-08-25         NaN         NaN 1998-04-30   \n",
       "40      -0.037872         4.0  1997-08-25         NaN         NaN 1998-05-31   \n",
       "41      -0.146986         4.0  1997-08-25         NaN         NaN 1998-06-30   \n",
       "42       0.008178         4.0  1997-08-25         NaN         NaN 1998-07-31   \n",
       "43      -0.064890         4.0  1997-08-25         NaN         NaN 1998-08-31   \n",
       "...           ...         ...         ...         ...         ...        ...   \n",
       "2109595 -0.082781        64.0  2023-11-21         0.0         0.0 2024-08-31   \n",
       "2109596  0.306859        64.0  2023-11-21         0.0         0.0 2024-09-30   \n",
       "2109597  2.400552        64.0  2023-11-21         0.0         0.0 2024-10-31   \n",
       "2109598 -0.217709        64.0  2023-11-21         0.0         0.0 2024-11-30   \n",
       "2109599 -0.271028        64.0  2023-11-21         0.0         0.0 2024-12-31   \n",
       "\n",
       "          rfmonth  list_month       ret  next_ret  Cumsum_tradingday  \\\n",
       "39       0.004249        85.0 -0.037165 -0.042121              235.0   \n",
       "40       0.004249        86.0 -0.042121 -0.151235              235.0   \n",
       "41       0.004249        87.0 -0.151235  0.004287              242.0   \n",
       "42       0.003891        88.0  0.004287 -0.068781              244.0   \n",
       "43       0.003891        89.0 -0.068781 -0.026815              244.0   \n",
       "...           ...         ...       ...       ...                ...   \n",
       "2109595  0.001118        50.0 -0.083900  0.305740              242.0   \n",
       "2109596  0.001118        51.0  0.305740  2.399433              241.0   \n",
       "2109597  0.001118        52.0  2.399433 -0.218827              238.0   \n",
       "2109598  0.001118        53.0 -0.218827 -0.272146              237.0   \n",
       "2109599  0.001118        54.0 -0.272146       NaN              238.0   \n",
       "\n",
       "              MKT      beta  data_count  \n",
       "39       0.085311  1.155400        40.0  \n",
       "40       0.050219  1.148875        41.0  \n",
       "41      -0.066011  1.167744        42.0  \n",
       "42      -0.016875  1.167735        43.0  \n",
       "43      -0.125304  1.152495        44.0  \n",
       "...           ...       ...         ...  \n",
       "2109595 -0.032586  0.725409        49.0  \n",
       "2109596  0.194874  0.983053        50.0  \n",
       "2109597 -0.004527  0.877199        51.0  \n",
       "2109598  0.008606  0.860914        52.0  \n",
       "2109599 -0.003797  0.872943        53.0  \n",
       "\n",
       "[630276 rows x 26 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stkcd</th>\n",
       "      <th>Trdmnt</th>\n",
       "      <th>Opndt</th>\n",
       "      <th>Mopnprc</th>\n",
       "      <th>Clsdt</th>\n",
       "      <th>Mclsprc</th>\n",
       "      <th>Mnshrtrd</th>\n",
       "      <th>Mnvaltrd</th>\n",
       "      <th>floatingvalue</th>\n",
       "      <th>totalvalue</th>\n",
       "      <th>Ndaytrd</th>\n",
       "      <th>Return</th>\n",
       "      <th>Mretnd</th>\n",
       "      <th>Markettype</th>\n",
       "      <th>Capchgdt</th>\n",
       "      <th>Ahshrtrd_M</th>\n",
       "      <th>Ahvaltrd_M</th>\n",
       "      <th>month</th>\n",
       "      <th>rfmonth</th>\n",
       "      <th>list_month</th>\n",
       "      <th>ret</th>\n",
       "      <th>next_ret</th>\n",
       "      <th>Cumsum_tradingday</th>\n",
       "      <th>MKT</th>\n",
       "      <th>beta</th>\n",
       "      <th>data_count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-04</td>\n",
       "      <td>1.0</td>\n",
       "      <td>21.50</td>\n",
       "      <td>30.0</td>\n",
       "      <td>20.86</td>\n",
       "      <td>135781919.0</td>\n",
       "      <td>2.931748e+09</td>\n",
       "      <td>2.233903e+10</td>\n",
       "      <td>3.237153e+10</td>\n",
       "      <td>21.0</td>\n",
       "      <td>-0.032916</td>\n",
       "      <td>-0.032916</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-04-30</td>\n",
       "      <td>0.004249</td>\n",
       "      <td>85.0</td>\n",
       "      <td>-0.037165</td>\n",
       "      <td>-0.042121</td>\n",
       "      <td>235.0</td>\n",
       "      <td>0.085311</td>\n",
       "      <td>1.155400</td>\n",
       "      <td>40.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-05</td>\n",
       "      <td>4.0</td>\n",
       "      <td>20.90</td>\n",
       "      <td>29.0</td>\n",
       "      <td>20.07</td>\n",
       "      <td>108329870.0</td>\n",
       "      <td>2.208166e+09</td>\n",
       "      <td>2.149301e+10</td>\n",
       "      <td>3.114557e+10</td>\n",
       "      <td>20.0</td>\n",
       "      <td>-0.037872</td>\n",
       "      <td>-0.037872</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-05-31</td>\n",
       "      <td>0.004249</td>\n",
       "      <td>86.0</td>\n",
       "      <td>-0.042121</td>\n",
       "      <td>-0.151235</td>\n",
       "      <td>235.0</td>\n",
       "      <td>0.050219</td>\n",
       "      <td>1.148875</td>\n",
       "      <td>41.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-06</td>\n",
       "      <td>1.0</td>\n",
       "      <td>20.10</td>\n",
       "      <td>30.0</td>\n",
       "      <td>17.12</td>\n",
       "      <td>91965412.0</td>\n",
       "      <td>1.746011e+09</td>\n",
       "      <td>1.833385e+10</td>\n",
       "      <td>2.656762e+10</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.146986</td>\n",
       "      <td>-0.146986</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-06-30</td>\n",
       "      <td>0.004249</td>\n",
       "      <td>87.0</td>\n",
       "      <td>-0.151235</td>\n",
       "      <td>0.004287</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.066011</td>\n",
       "      <td>1.167744</td>\n",
       "      <td>42.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>17.58</td>\n",
       "      <td>31.0</td>\n",
       "      <td>17.26</td>\n",
       "      <td>102722557.0</td>\n",
       "      <td>1.740172e+09</td>\n",
       "      <td>1.848378e+10</td>\n",
       "      <td>2.678488e+10</td>\n",
       "      <td>23.0</td>\n",
       "      <td>0.008178</td>\n",
       "      <td>0.008178</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-07-31</td>\n",
       "      <td>0.003891</td>\n",
       "      <td>88.0</td>\n",
       "      <td>0.004287</td>\n",
       "      <td>-0.068781</td>\n",
       "      <td>244.0</td>\n",
       "      <td>-0.016875</td>\n",
       "      <td>1.167735</td>\n",
       "      <td>43.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-08</td>\n",
       "      <td>3.0</td>\n",
       "      <td>17.28</td>\n",
       "      <td>31.0</td>\n",
       "      <td>16.14</td>\n",
       "      <td>71852835.0</td>\n",
       "      <td>1.172085e+09</td>\n",
       "      <td>1.728437e+10</td>\n",
       "      <td>2.504681e+10</td>\n",
       "      <td>21.0</td>\n",
       "      <td>-0.064890</td>\n",
       "      <td>-0.064890</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-08-31</td>\n",
       "      <td>0.003891</td>\n",
       "      <td>89.0</td>\n",
       "      <td>-0.068781</td>\n",
       "      <td>-0.026815</td>\n",
       "      <td>244.0</td>\n",
       "      <td>-0.125304</td>\n",
       "      <td>1.152495</td>\n",
       "      <td>44.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2109595</th>\n",
       "      <td>920964</td>\n",
       "      <td>2024-08</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.03</td>\n",
       "      <td>30.0</td>\n",
       "      <td>2.77</td>\n",
       "      <td>36090137.0</td>\n",
       "      <td>1.037190e+08</td>\n",
       "      <td>4.969253e+08</td>\n",
       "      <td>7.235462e+08</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.082782</td>\n",
       "      <td>-0.082781</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2023-11-21</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-08-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>50.0</td>\n",
       "      <td>-0.083900</td>\n",
       "      <td>0.305740</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.032586</td>\n",
       "      <td>0.725409</td>\n",
       "      <td>49.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2109596</th>\n",
       "      <td>920964</td>\n",
       "      <td>2024-09</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.80</td>\n",
       "      <td>30.0</td>\n",
       "      <td>3.62</td>\n",
       "      <td>49005108.0</td>\n",
       "      <td>1.488490e+08</td>\n",
       "      <td>6.494114e+08</td>\n",
       "      <td>9.455730e+08</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0.306858</td>\n",
       "      <td>0.306859</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2023-11-21</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-09-30</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>51.0</td>\n",
       "      <td>0.305740</td>\n",
       "      <td>2.399433</td>\n",
       "      <td>241.0</td>\n",
       "      <td>0.194874</td>\n",
       "      <td>0.983053</td>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2109597</th>\n",
       "      <td>920964</td>\n",
       "      <td>2024-10</td>\n",
       "      <td>8.0</td>\n",
       "      <td>4.36</td>\n",
       "      <td>31.0</td>\n",
       "      <td>12.31</td>\n",
       "      <td>219750340.0</td>\n",
       "      <td>1.478570e+09</td>\n",
       "      <td>2.208358e+09</td>\n",
       "      <td>3.215470e+09</td>\n",
       "      <td>14.0</td>\n",
       "      <td>2.400551</td>\n",
       "      <td>2.400552</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2023-11-21</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-10-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>52.0</td>\n",
       "      <td>2.399433</td>\n",
       "      <td>-0.218827</td>\n",
       "      <td>238.0</td>\n",
       "      <td>-0.004527</td>\n",
       "      <td>0.877199</td>\n",
       "      <td>51.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2109598</th>\n",
       "      <td>920964</td>\n",
       "      <td>2024-11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>13.40</td>\n",
       "      <td>29.0</td>\n",
       "      <td>9.63</td>\n",
       "      <td>628989566.0</td>\n",
       "      <td>7.034681e+09</td>\n",
       "      <td>1.727578e+09</td>\n",
       "      <td>2.515433e+09</td>\n",
       "      <td>21.0</td>\n",
       "      <td>-0.217709</td>\n",
       "      <td>-0.217709</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2023-11-21</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-11-30</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>53.0</td>\n",
       "      <td>-0.218827</td>\n",
       "      <td>-0.272146</td>\n",
       "      <td>237.0</td>\n",
       "      <td>0.008606</td>\n",
       "      <td>0.860914</td>\n",
       "      <td>52.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2109599</th>\n",
       "      <td>920964</td>\n",
       "      <td>2024-12</td>\n",
       "      <td>2.0</td>\n",
       "      <td>9.63</td>\n",
       "      <td>31.0</td>\n",
       "      <td>7.02</td>\n",
       "      <td>251277231.0</td>\n",
       "      <td>2.284998e+09</td>\n",
       "      <td>1.259356e+09</td>\n",
       "      <td>1.833680e+09</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.271028</td>\n",
       "      <td>-0.271028</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2023-11-21</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>54.0</td>\n",
       "      <td>-0.272146</td>\n",
       "      <td>NaN</td>\n",
       "      <td>238.0</td>\n",
       "      <td>-0.003797</td>\n",
       "      <td>0.872943</td>\n",
       "      <td>53.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>630276 rows × 26 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 42
  },
  {
   "cell_type": "code",
   "id": "9040e4f9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:08:29.922777Z",
     "start_time": "2025-11-18T06:08:29.438810Z"
    }
   },
   "source": [
    "cross_beta = cross_beta[cross_beta['Ndaytrd'] >= 7]\n",
    "cross_beta = cross_beta[cross_beta['Clsdt'] >= 5]\n",
    "cross_beta = cross_beta[cross_beta['list_month'] > 6]\n",
    "cross_beta = cross_beta[cross_beta['Cumsum_tradingday'] >= 100]\n",
    "cross_beta = cross_beta[(cross_beta['Markettype'] == 1) | (cross_beta['Markettype'] == 4) | (cross_beta['Markettype'] == 6)]\n",
    "cross_beta"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          Stkcd   Trdmnt  Opndt  Mopnprc  Clsdt  Mclsprc     Mnshrtrd  \\\n",
       "39       000001  1998-04    1.0    21.50   30.0    20.86  135781919.0   \n",
       "40       000001  1998-05    4.0    20.90   29.0    20.07  108329870.0   \n",
       "41       000001  1998-06    1.0    20.10   30.0    17.12   91965412.0   \n",
       "42       000001  1998-07    1.0    17.58   31.0    17.26  102722557.0   \n",
       "43       000001  1998-08    3.0    17.28   31.0    16.14   71852835.0   \n",
       "...         ...      ...    ...      ...    ...      ...          ...   \n",
       "1776239  605507  2024-12    2.0    22.07   31.0    20.83   61892279.0   \n",
       "1777679  605577  2024-12    2.0    16.93   31.0    14.53  228878763.0   \n",
       "1778039  605580  2024-12    2.0     9.88   31.0    11.08  154703585.0   \n",
       "1778399  605588  2024-12    2.0    47.96   31.0    40.76   41273627.0   \n",
       "1778759  605589  2024-12    2.0    25.08   31.0    23.58  210979113.0   \n",
       "\n",
       "             Mnvaltrd  floatingvalue    totalvalue  Ndaytrd    Return  \\\n",
       "39       2.931748e+09   2.233903e+10  3.237153e+10     21.0 -0.032916   \n",
       "40       2.208166e+09   2.149301e+10  3.114557e+10     20.0 -0.037872   \n",
       "41       1.746011e+09   1.833385e+10  2.656762e+10     22.0 -0.146986   \n",
       "42       1.740172e+09   1.848378e+10  2.678488e+10     23.0  0.008178   \n",
       "43       1.172085e+09   1.728437e+10  2.504681e+10     21.0 -0.064890   \n",
       "...               ...            ...           ...      ...       ...   \n",
       "1776239  1.307565e+09   6.284873e+09  1.164029e+10     22.0 -0.047989   \n",
       "1777679  3.862518e+09   6.457778e+09  6.457778e+09     22.0 -0.135119   \n",
       "1778039  1.623849e+09   3.102400e+09  3.102400e+09     22.0  0.132924   \n",
       "1778399  1.873835e+09   2.979538e+09  2.995151e+09     22.0 -0.150302   \n",
       "1778759  5.061180e+09   1.835846e+10  1.995943e+10     22.0 -0.023603   \n",
       "\n",
       "           Mretnd  Markettype    Capchgdt  Ahshrtrd_M  Ahvaltrd_M      month  \\\n",
       "39      -0.032916         4.0  1997-08-25         NaN         NaN 1998-04-30   \n",
       "40      -0.037872         4.0  1997-08-25         NaN         NaN 1998-05-31   \n",
       "41      -0.146986         4.0  1997-08-25         NaN         NaN 1998-06-30   \n",
       "42       0.008178         4.0  1997-08-25         NaN         NaN 1998-07-31   \n",
       "43      -0.064890         4.0  1997-08-25         NaN         NaN 1998-08-31   \n",
       "...           ...         ...         ...         ...         ...        ...   \n",
       "1776239 -0.047989         1.0  2023-02-02         0.0         0.0 2024-12-31   \n",
       "1777679 -0.135119         1.0  2024-09-30         0.0         0.0 2024-12-31   \n",
       "1778039  0.132924         1.0  2024-08-26         0.0         0.0 2024-12-31   \n",
       "1778399 -0.150302         1.0  2024-12-17         0.0         0.0 2024-12-31   \n",
       "1778759 -0.023602         1.0  2024-11-07         0.0         0.0 2024-12-31   \n",
       "\n",
       "          rfmonth  list_month       ret  next_ret  Cumsum_tradingday  \\\n",
       "39       0.004249        85.0 -0.037165 -0.042121              235.0   \n",
       "40       0.004249        86.0 -0.042121 -0.151235              235.0   \n",
       "41       0.004249        87.0 -0.151235  0.004287              242.0   \n",
       "42       0.003891        88.0  0.004287 -0.068781              244.0   \n",
       "43       0.003891        89.0 -0.068781 -0.026815              244.0   \n",
       "...           ...         ...       ...       ...                ...   \n",
       "1776239  0.001118        41.0 -0.049107       NaN              242.0   \n",
       "1777679  0.001118        41.0 -0.136237       NaN              242.0   \n",
       "1778039  0.001118        41.0  0.131806       NaN              242.0   \n",
       "1778399  0.001118        41.0 -0.151420       NaN              242.0   \n",
       "1778759  0.001118        41.0 -0.024721       NaN              242.0   \n",
       "\n",
       "              MKT      beta  data_count  \n",
       "39       0.085311  1.155400        40.0  \n",
       "40       0.050219  1.148875        41.0  \n",
       "41      -0.066011  1.167744        42.0  \n",
       "42      -0.016875  1.167735        43.0  \n",
       "43      -0.125304  1.152495        44.0  \n",
       "...           ...       ...         ...  \n",
       "1776239 -0.003797  0.604456        40.0  \n",
       "1777679 -0.003797  0.764475        40.0  \n",
       "1778039 -0.003797  0.785369        40.0  \n",
       "1778399 -0.003797  0.976478        40.0  \n",
       "1778759 -0.003797  1.717052        40.0  \n",
       "\n",
       "[502366 rows x 26 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stkcd</th>\n",
       "      <th>Trdmnt</th>\n",
       "      <th>Opndt</th>\n",
       "      <th>Mopnprc</th>\n",
       "      <th>Clsdt</th>\n",
       "      <th>Mclsprc</th>\n",
       "      <th>Mnshrtrd</th>\n",
       "      <th>Mnvaltrd</th>\n",
       "      <th>floatingvalue</th>\n",
       "      <th>totalvalue</th>\n",
       "      <th>Ndaytrd</th>\n",
       "      <th>Return</th>\n",
       "      <th>Mretnd</th>\n",
       "      <th>Markettype</th>\n",
       "      <th>Capchgdt</th>\n",
       "      <th>Ahshrtrd_M</th>\n",
       "      <th>Ahvaltrd_M</th>\n",
       "      <th>month</th>\n",
       "      <th>rfmonth</th>\n",
       "      <th>list_month</th>\n",
       "      <th>ret</th>\n",
       "      <th>next_ret</th>\n",
       "      <th>Cumsum_tradingday</th>\n",
       "      <th>MKT</th>\n",
       "      <th>beta</th>\n",
       "      <th>data_count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-04</td>\n",
       "      <td>1.0</td>\n",
       "      <td>21.50</td>\n",
       "      <td>30.0</td>\n",
       "      <td>20.86</td>\n",
       "      <td>135781919.0</td>\n",
       "      <td>2.931748e+09</td>\n",
       "      <td>2.233903e+10</td>\n",
       "      <td>3.237153e+10</td>\n",
       "      <td>21.0</td>\n",
       "      <td>-0.032916</td>\n",
       "      <td>-0.032916</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-04-30</td>\n",
       "      <td>0.004249</td>\n",
       "      <td>85.0</td>\n",
       "      <td>-0.037165</td>\n",
       "      <td>-0.042121</td>\n",
       "      <td>235.0</td>\n",
       "      <td>0.085311</td>\n",
       "      <td>1.155400</td>\n",
       "      <td>40.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-05</td>\n",
       "      <td>4.0</td>\n",
       "      <td>20.90</td>\n",
       "      <td>29.0</td>\n",
       "      <td>20.07</td>\n",
       "      <td>108329870.0</td>\n",
       "      <td>2.208166e+09</td>\n",
       "      <td>2.149301e+10</td>\n",
       "      <td>3.114557e+10</td>\n",
       "      <td>20.0</td>\n",
       "      <td>-0.037872</td>\n",
       "      <td>-0.037872</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-05-31</td>\n",
       "      <td>0.004249</td>\n",
       "      <td>86.0</td>\n",
       "      <td>-0.042121</td>\n",
       "      <td>-0.151235</td>\n",
       "      <td>235.0</td>\n",
       "      <td>0.050219</td>\n",
       "      <td>1.148875</td>\n",
       "      <td>41.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-06</td>\n",
       "      <td>1.0</td>\n",
       "      <td>20.10</td>\n",
       "      <td>30.0</td>\n",
       "      <td>17.12</td>\n",
       "      <td>91965412.0</td>\n",
       "      <td>1.746011e+09</td>\n",
       "      <td>1.833385e+10</td>\n",
       "      <td>2.656762e+10</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.146986</td>\n",
       "      <td>-0.146986</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-06-30</td>\n",
       "      <td>0.004249</td>\n",
       "      <td>87.0</td>\n",
       "      <td>-0.151235</td>\n",
       "      <td>0.004287</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.066011</td>\n",
       "      <td>1.167744</td>\n",
       "      <td>42.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>17.58</td>\n",
       "      <td>31.0</td>\n",
       "      <td>17.26</td>\n",
       "      <td>102722557.0</td>\n",
       "      <td>1.740172e+09</td>\n",
       "      <td>1.848378e+10</td>\n",
       "      <td>2.678488e+10</td>\n",
       "      <td>23.0</td>\n",
       "      <td>0.008178</td>\n",
       "      <td>0.008178</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-07-31</td>\n",
       "      <td>0.003891</td>\n",
       "      <td>88.0</td>\n",
       "      <td>0.004287</td>\n",
       "      <td>-0.068781</td>\n",
       "      <td>244.0</td>\n",
       "      <td>-0.016875</td>\n",
       "      <td>1.167735</td>\n",
       "      <td>43.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-08</td>\n",
       "      <td>3.0</td>\n",
       "      <td>17.28</td>\n",
       "      <td>31.0</td>\n",
       "      <td>16.14</td>\n",
       "      <td>71852835.0</td>\n",
       "      <td>1.172085e+09</td>\n",
       "      <td>1.728437e+10</td>\n",
       "      <td>2.504681e+10</td>\n",
       "      <td>21.0</td>\n",
       "      <td>-0.064890</td>\n",
       "      <td>-0.064890</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-08-31</td>\n",
       "      <td>0.003891</td>\n",
       "      <td>89.0</td>\n",
       "      <td>-0.068781</td>\n",
       "      <td>-0.026815</td>\n",
       "      <td>244.0</td>\n",
       "      <td>-0.125304</td>\n",
       "      <td>1.152495</td>\n",
       "      <td>44.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1776239</th>\n",
       "      <td>605507</td>\n",
       "      <td>2024-12</td>\n",
       "      <td>2.0</td>\n",
       "      <td>22.07</td>\n",
       "      <td>31.0</td>\n",
       "      <td>20.83</td>\n",
       "      <td>61892279.0</td>\n",
       "      <td>1.307565e+09</td>\n",
       "      <td>6.284873e+09</td>\n",
       "      <td>1.164029e+10</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.047989</td>\n",
       "      <td>-0.047989</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2023-02-02</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>41.0</td>\n",
       "      <td>-0.049107</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.003797</td>\n",
       "      <td>0.604456</td>\n",
       "      <td>40.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1777679</th>\n",
       "      <td>605577</td>\n",
       "      <td>2024-12</td>\n",
       "      <td>2.0</td>\n",
       "      <td>16.93</td>\n",
       "      <td>31.0</td>\n",
       "      <td>14.53</td>\n",
       "      <td>228878763.0</td>\n",
       "      <td>3.862518e+09</td>\n",
       "      <td>6.457778e+09</td>\n",
       "      <td>6.457778e+09</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.135119</td>\n",
       "      <td>-0.135119</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-09-30</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>41.0</td>\n",
       "      <td>-0.136237</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.003797</td>\n",
       "      <td>0.764475</td>\n",
       "      <td>40.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1778039</th>\n",
       "      <td>605580</td>\n",
       "      <td>2024-12</td>\n",
       "      <td>2.0</td>\n",
       "      <td>9.88</td>\n",
       "      <td>31.0</td>\n",
       "      <td>11.08</td>\n",
       "      <td>154703585.0</td>\n",
       "      <td>1.623849e+09</td>\n",
       "      <td>3.102400e+09</td>\n",
       "      <td>3.102400e+09</td>\n",
       "      <td>22.0</td>\n",
       "      <td>0.132924</td>\n",
       "      <td>0.132924</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-08-26</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>41.0</td>\n",
       "      <td>0.131806</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.003797</td>\n",
       "      <td>0.785369</td>\n",
       "      <td>40.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1778399</th>\n",
       "      <td>605588</td>\n",
       "      <td>2024-12</td>\n",
       "      <td>2.0</td>\n",
       "      <td>47.96</td>\n",
       "      <td>31.0</td>\n",
       "      <td>40.76</td>\n",
       "      <td>41273627.0</td>\n",
       "      <td>1.873835e+09</td>\n",
       "      <td>2.979538e+09</td>\n",
       "      <td>2.995151e+09</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.150302</td>\n",
       "      <td>-0.150302</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-12-17</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>41.0</td>\n",
       "      <td>-0.151420</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.003797</td>\n",
       "      <td>0.976478</td>\n",
       "      <td>40.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1778759</th>\n",
       "      <td>605589</td>\n",
       "      <td>2024-12</td>\n",
       "      <td>2.0</td>\n",
       "      <td>25.08</td>\n",
       "      <td>31.0</td>\n",
       "      <td>23.58</td>\n",
       "      <td>210979113.0</td>\n",
       "      <td>5.061180e+09</td>\n",
       "      <td>1.835846e+10</td>\n",
       "      <td>1.995943e+10</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.023603</td>\n",
       "      <td>-0.023602</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-11-07</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>41.0</td>\n",
       "      <td>-0.024721</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.003797</td>\n",
       "      <td>1.717052</td>\n",
       "      <td>40.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>502366 rows × 26 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 43
  },
  {
   "cell_type": "code",
   "id": "6891b17e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:08:32.391728Z",
     "start_time": "2025-11-18T06:08:32.300836Z"
    }
   },
   "source": [
    "fenweishu = pd.DataFrame(\n",
    "    cross_beta.groupby(['month'])['beta'].quantile([0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]))\n",
    "fenweishu = fenweishu.reset_index()\n",
    "fenweishu = fenweishu.pivot_table(index='month',columns='level_1',values='beta')\n",
    "fenweishu.columns = ['one','two','three','four','five','six','seven','eight','nine']\n",
    "fenweishu"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                 one       two     three      four      five       six  \\\n",
       "month                                                                    \n",
       "1998-04-30  0.707349  0.781328  0.847070  0.918619  0.986271  1.053890   \n",
       "1998-05-31  0.710665  0.786197  0.852553  0.921351  0.992781  1.057841   \n",
       "1998-06-30  0.720040  0.782919  0.868701  0.920163  0.991961  1.060550   \n",
       "1998-07-31  0.708720  0.780527  0.862017  0.919622  0.994468  1.065228   \n",
       "1998-08-31  0.708167  0.774056  0.849154  0.922411  0.989177  1.068256   \n",
       "...              ...       ...       ...       ...       ...       ...   \n",
       "2024-08-31  0.527212  0.690668  0.808727  0.903639  1.005142  1.103606   \n",
       "2024-09-30  0.613942  0.747197  0.844887  0.929128  1.020618  1.112952   \n",
       "2024-10-31  0.609603  0.741257  0.840805  0.923708  1.015869  1.106596   \n",
       "2024-11-30  0.611609  0.744125  0.844421  0.925974  1.016210  1.107136   \n",
       "2024-12-31  0.609035  0.742393  0.841648  0.923380  1.017028  1.108646   \n",
       "\n",
       "               seven     eight      nine  \n",
       "month                                     \n",
       "1998-04-30  1.147653  1.263057  1.403121  \n",
       "1998-05-31  1.143836  1.276886  1.410021  \n",
       "1998-06-30  1.148936  1.277518  1.404875  \n",
       "1998-07-31  1.141907  1.270604  1.398097  \n",
       "1998-08-31  1.151609  1.247518  1.387920  \n",
       "...              ...       ...       ...  \n",
       "2024-08-31  1.236126  1.375513  1.610912  \n",
       "2024-09-30  1.212442  1.330217  1.528623  \n",
       "2024-10-31  1.205720  1.327996  1.526917  \n",
       "2024-11-30  1.206734  1.328866  1.524422  \n",
       "2024-12-31  1.208609  1.329028  1.529482  \n",
       "\n",
       "[321 rows x 9 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "      <th>seven</th>\n",
       "      <th>eight</th>\n",
       "      <th>nine</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>month</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1998-04-30</th>\n",
       "      <td>0.707349</td>\n",
       "      <td>0.781328</td>\n",
       "      <td>0.847070</td>\n",
       "      <td>0.918619</td>\n",
       "      <td>0.986271</td>\n",
       "      <td>1.053890</td>\n",
       "      <td>1.147653</td>\n",
       "      <td>1.263057</td>\n",
       "      <td>1.403121</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-05-31</th>\n",
       "      <td>0.710665</td>\n",
       "      <td>0.786197</td>\n",
       "      <td>0.852553</td>\n",
       "      <td>0.921351</td>\n",
       "      <td>0.992781</td>\n",
       "      <td>1.057841</td>\n",
       "      <td>1.143836</td>\n",
       "      <td>1.276886</td>\n",
       "      <td>1.410021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06-30</th>\n",
       "      <td>0.720040</td>\n",
       "      <td>0.782919</td>\n",
       "      <td>0.868701</td>\n",
       "      <td>0.920163</td>\n",
       "      <td>0.991961</td>\n",
       "      <td>1.060550</td>\n",
       "      <td>1.148936</td>\n",
       "      <td>1.277518</td>\n",
       "      <td>1.404875</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-07-31</th>\n",
       "      <td>0.708720</td>\n",
       "      <td>0.780527</td>\n",
       "      <td>0.862017</td>\n",
       "      <td>0.919622</td>\n",
       "      <td>0.994468</td>\n",
       "      <td>1.065228</td>\n",
       "      <td>1.141907</td>\n",
       "      <td>1.270604</td>\n",
       "      <td>1.398097</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-08-31</th>\n",
       "      <td>0.708167</td>\n",
       "      <td>0.774056</td>\n",
       "      <td>0.849154</td>\n",
       "      <td>0.922411</td>\n",
       "      <td>0.989177</td>\n",
       "      <td>1.068256</td>\n",
       "      <td>1.151609</td>\n",
       "      <td>1.247518</td>\n",
       "      <td>1.387920</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-08-31</th>\n",
       "      <td>0.527212</td>\n",
       "      <td>0.690668</td>\n",
       "      <td>0.808727</td>\n",
       "      <td>0.903639</td>\n",
       "      <td>1.005142</td>\n",
       "      <td>1.103606</td>\n",
       "      <td>1.236126</td>\n",
       "      <td>1.375513</td>\n",
       "      <td>1.610912</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-30</th>\n",
       "      <td>0.613942</td>\n",
       "      <td>0.747197</td>\n",
       "      <td>0.844887</td>\n",
       "      <td>0.929128</td>\n",
       "      <td>1.020618</td>\n",
       "      <td>1.112952</td>\n",
       "      <td>1.212442</td>\n",
       "      <td>1.330217</td>\n",
       "      <td>1.528623</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-10-31</th>\n",
       "      <td>0.609603</td>\n",
       "      <td>0.741257</td>\n",
       "      <td>0.840805</td>\n",
       "      <td>0.923708</td>\n",
       "      <td>1.015869</td>\n",
       "      <td>1.106596</td>\n",
       "      <td>1.205720</td>\n",
       "      <td>1.327996</td>\n",
       "      <td>1.526917</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-30</th>\n",
       "      <td>0.611609</td>\n",
       "      <td>0.744125</td>\n",
       "      <td>0.844421</td>\n",
       "      <td>0.925974</td>\n",
       "      <td>1.016210</td>\n",
       "      <td>1.107136</td>\n",
       "      <td>1.206734</td>\n",
       "      <td>1.328866</td>\n",
       "      <td>1.524422</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>0.609035</td>\n",
       "      <td>0.742393</td>\n",
       "      <td>0.841648</td>\n",
       "      <td>0.923380</td>\n",
       "      <td>1.017028</td>\n",
       "      <td>1.108646</td>\n",
       "      <td>1.208609</td>\n",
       "      <td>1.329028</td>\n",
       "      <td>1.529482</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>321 rows × 9 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 44
  },
  {
   "cell_type": "code",
   "id": "2d76d854",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:08:35.632812Z",
     "start_time": "2025-11-18T06:08:35.498850Z"
    }
   },
   "source": [
    "portfolio = pd.merge(cross_beta,fenweishu,on='month')\n",
    "portfolio"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         Stkcd   Trdmnt  Opndt  Mopnprc  Clsdt  Mclsprc     Mnshrtrd  \\\n",
       "0       000001  1998-04    1.0    21.50   30.0    20.86  135781919.0   \n",
       "1       000001  1998-05    4.0    20.90   29.0    20.07  108329870.0   \n",
       "2       000001  1998-06    1.0    20.10   30.0    17.12   91965412.0   \n",
       "3       000001  1998-07    1.0    17.58   31.0    17.26  102722557.0   \n",
       "4       000001  1998-08    3.0    17.28   31.0    16.14   71852835.0   \n",
       "...        ...      ...    ...      ...    ...      ...          ...   \n",
       "502361  605507  2024-12    2.0    22.07   31.0    20.83   61892279.0   \n",
       "502362  605577  2024-12    2.0    16.93   31.0    14.53  228878763.0   \n",
       "502363  605580  2024-12    2.0     9.88   31.0    11.08  154703585.0   \n",
       "502364  605588  2024-12    2.0    47.96   31.0    40.76   41273627.0   \n",
       "502365  605589  2024-12    2.0    25.08   31.0    23.58  210979113.0   \n",
       "\n",
       "            Mnvaltrd  floatingvalue    totalvalue  Ndaytrd    Return  \\\n",
       "0       2.931748e+09   2.233903e+10  3.237153e+10     21.0 -0.032916   \n",
       "1       2.208166e+09   2.149301e+10  3.114557e+10     20.0 -0.037872   \n",
       "2       1.746011e+09   1.833385e+10  2.656762e+10     22.0 -0.146986   \n",
       "3       1.740172e+09   1.848378e+10  2.678488e+10     23.0  0.008178   \n",
       "4       1.172085e+09   1.728437e+10  2.504681e+10     21.0 -0.064890   \n",
       "...              ...            ...           ...      ...       ...   \n",
       "502361  1.307565e+09   6.284873e+09  1.164029e+10     22.0 -0.047989   \n",
       "502362  3.862518e+09   6.457778e+09  6.457778e+09     22.0 -0.135119   \n",
       "502363  1.623849e+09   3.102400e+09  3.102400e+09     22.0  0.132924   \n",
       "502364  1.873835e+09   2.979538e+09  2.995151e+09     22.0 -0.150302   \n",
       "502365  5.061180e+09   1.835846e+10  1.995943e+10     22.0 -0.023603   \n",
       "\n",
       "          Mretnd  Markettype    Capchgdt  Ahshrtrd_M  Ahvaltrd_M      month  \\\n",
       "0      -0.032916         4.0  1997-08-25         NaN         NaN 1998-04-30   \n",
       "1      -0.037872         4.0  1997-08-25         NaN         NaN 1998-05-31   \n",
       "2      -0.146986         4.0  1997-08-25         NaN         NaN 1998-06-30   \n",
       "3       0.008178         4.0  1997-08-25         NaN         NaN 1998-07-31   \n",
       "4      -0.064890         4.0  1997-08-25         NaN         NaN 1998-08-31   \n",
       "...          ...         ...         ...         ...         ...        ...   \n",
       "502361 -0.047989         1.0  2023-02-02         0.0         0.0 2024-12-31   \n",
       "502362 -0.135119         1.0  2024-09-30         0.0         0.0 2024-12-31   \n",
       "502363  0.132924         1.0  2024-08-26         0.0         0.0 2024-12-31   \n",
       "502364 -0.150302         1.0  2024-12-17         0.0         0.0 2024-12-31   \n",
       "502365 -0.023602         1.0  2024-11-07         0.0         0.0 2024-12-31   \n",
       "\n",
       "         rfmonth  list_month       ret  next_ret  Cumsum_tradingday       MKT  \\\n",
       "0       0.004249        85.0 -0.037165 -0.042121              235.0  0.085311   \n",
       "1       0.004249        86.0 -0.042121 -0.151235              235.0  0.050219   \n",
       "2       0.004249        87.0 -0.151235  0.004287              242.0 -0.066011   \n",
       "3       0.003891        88.0  0.004287 -0.068781              244.0 -0.016875   \n",
       "4       0.003891        89.0 -0.068781 -0.026815              244.0 -0.125304   \n",
       "...          ...         ...       ...       ...                ...       ...   \n",
       "502361  0.001118        41.0 -0.049107       NaN              242.0 -0.003797   \n",
       "502362  0.001118        41.0 -0.136237       NaN              242.0 -0.003797   \n",
       "502363  0.001118        41.0  0.131806       NaN              242.0 -0.003797   \n",
       "502364  0.001118        41.0 -0.151420       NaN              242.0 -0.003797   \n",
       "502365  0.001118        41.0 -0.024721       NaN              242.0 -0.003797   \n",
       "\n",
       "            beta  data_count       one       two     three      four  \\\n",
       "0       1.155400        40.0  0.707349  0.781328  0.847070  0.918619   \n",
       "1       1.148875        41.0  0.710665  0.786197  0.852553  0.921351   \n",
       "2       1.167744        42.0  0.720040  0.782919  0.868701  0.920163   \n",
       "3       1.167735        43.0  0.708720  0.780527  0.862017  0.919622   \n",
       "4       1.152495        44.0  0.708167  0.774056  0.849154  0.922411   \n",
       "...          ...         ...       ...       ...       ...       ...   \n",
       "502361  0.604456        40.0  0.609035  0.742393  0.841648  0.923380   \n",
       "502362  0.764475        40.0  0.609035  0.742393  0.841648  0.923380   \n",
       "502363  0.785369        40.0  0.609035  0.742393  0.841648  0.923380   \n",
       "502364  0.976478        40.0  0.609035  0.742393  0.841648  0.923380   \n",
       "502365  1.717052        40.0  0.609035  0.742393  0.841648  0.923380   \n",
       "\n",
       "            five       six     seven     eight      nine  \n",
       "0       0.986271  1.053890  1.147653  1.263057  1.403121  \n",
       "1       0.992781  1.057841  1.143836  1.276886  1.410021  \n",
       "2       0.991961  1.060550  1.148936  1.277518  1.404875  \n",
       "3       0.994468  1.065228  1.141907  1.270604  1.398097  \n",
       "4       0.989177  1.068256  1.151609  1.247518  1.387920  \n",
       "...          ...       ...       ...       ...       ...  \n",
       "502361  1.017028  1.108646  1.208609  1.329028  1.529482  \n",
       "502362  1.017028  1.108646  1.208609  1.329028  1.529482  \n",
       "502363  1.017028  1.108646  1.208609  1.329028  1.529482  \n",
       "502364  1.017028  1.108646  1.208609  1.329028  1.529482  \n",
       "502365  1.017028  1.108646  1.208609  1.329028  1.529482  \n",
       "\n",
       "[502366 rows x 35 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stkcd</th>\n",
       "      <th>Trdmnt</th>\n",
       "      <th>Opndt</th>\n",
       "      <th>Mopnprc</th>\n",
       "      <th>Clsdt</th>\n",
       "      <th>Mclsprc</th>\n",
       "      <th>Mnshrtrd</th>\n",
       "      <th>Mnvaltrd</th>\n",
       "      <th>floatingvalue</th>\n",
       "      <th>totalvalue</th>\n",
       "      <th>Ndaytrd</th>\n",
       "      <th>Return</th>\n",
       "      <th>Mretnd</th>\n",
       "      <th>Markettype</th>\n",
       "      <th>Capchgdt</th>\n",
       "      <th>Ahshrtrd_M</th>\n",
       "      <th>Ahvaltrd_M</th>\n",
       "      <th>month</th>\n",
       "      <th>rfmonth</th>\n",
       "      <th>list_month</th>\n",
       "      <th>ret</th>\n",
       "      <th>next_ret</th>\n",
       "      <th>Cumsum_tradingday</th>\n",
       "      <th>MKT</th>\n",
       "      <th>beta</th>\n",
       "      <th>data_count</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "      <th>seven</th>\n",
       "      <th>eight</th>\n",
       "      <th>nine</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-04</td>\n",
       "      <td>1.0</td>\n",
       "      <td>21.50</td>\n",
       "      <td>30.0</td>\n",
       "      <td>20.86</td>\n",
       "      <td>135781919.0</td>\n",
       "      <td>2.931748e+09</td>\n",
       "      <td>2.233903e+10</td>\n",
       "      <td>3.237153e+10</td>\n",
       "      <td>21.0</td>\n",
       "      <td>-0.032916</td>\n",
       "      <td>-0.032916</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-04-30</td>\n",
       "      <td>0.004249</td>\n",
       "      <td>85.0</td>\n",
       "      <td>-0.037165</td>\n",
       "      <td>-0.042121</td>\n",
       "      <td>235.0</td>\n",
       "      <td>0.085311</td>\n",
       "      <td>1.155400</td>\n",
       "      <td>40.0</td>\n",
       "      <td>0.707349</td>\n",
       "      <td>0.781328</td>\n",
       "      <td>0.847070</td>\n",
       "      <td>0.918619</td>\n",
       "      <td>0.986271</td>\n",
       "      <td>1.053890</td>\n",
       "      <td>1.147653</td>\n",
       "      <td>1.263057</td>\n",
       "      <td>1.403121</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-05</td>\n",
       "      <td>4.0</td>\n",
       "      <td>20.90</td>\n",
       "      <td>29.0</td>\n",
       "      <td>20.07</td>\n",
       "      <td>108329870.0</td>\n",
       "      <td>2.208166e+09</td>\n",
       "      <td>2.149301e+10</td>\n",
       "      <td>3.114557e+10</td>\n",
       "      <td>20.0</td>\n",
       "      <td>-0.037872</td>\n",
       "      <td>-0.037872</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-05-31</td>\n",
       "      <td>0.004249</td>\n",
       "      <td>86.0</td>\n",
       "      <td>-0.042121</td>\n",
       "      <td>-0.151235</td>\n",
       "      <td>235.0</td>\n",
       "      <td>0.050219</td>\n",
       "      <td>1.148875</td>\n",
       "      <td>41.0</td>\n",
       "      <td>0.710665</td>\n",
       "      <td>0.786197</td>\n",
       "      <td>0.852553</td>\n",
       "      <td>0.921351</td>\n",
       "      <td>0.992781</td>\n",
       "      <td>1.057841</td>\n",
       "      <td>1.143836</td>\n",
       "      <td>1.276886</td>\n",
       "      <td>1.410021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-06</td>\n",
       "      <td>1.0</td>\n",
       "      <td>20.10</td>\n",
       "      <td>30.0</td>\n",
       "      <td>17.12</td>\n",
       "      <td>91965412.0</td>\n",
       "      <td>1.746011e+09</td>\n",
       "      <td>1.833385e+10</td>\n",
       "      <td>2.656762e+10</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.146986</td>\n",
       "      <td>-0.146986</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-06-30</td>\n",
       "      <td>0.004249</td>\n",
       "      <td>87.0</td>\n",
       "      <td>-0.151235</td>\n",
       "      <td>0.004287</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.066011</td>\n",
       "      <td>1.167744</td>\n",
       "      <td>42.0</td>\n",
       "      <td>0.720040</td>\n",
       "      <td>0.782919</td>\n",
       "      <td>0.868701</td>\n",
       "      <td>0.920163</td>\n",
       "      <td>0.991961</td>\n",
       "      <td>1.060550</td>\n",
       "      <td>1.148936</td>\n",
       "      <td>1.277518</td>\n",
       "      <td>1.404875</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>17.58</td>\n",
       "      <td>31.0</td>\n",
       "      <td>17.26</td>\n",
       "      <td>102722557.0</td>\n",
       "      <td>1.740172e+09</td>\n",
       "      <td>1.848378e+10</td>\n",
       "      <td>2.678488e+10</td>\n",
       "      <td>23.0</td>\n",
       "      <td>0.008178</td>\n",
       "      <td>0.008178</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-07-31</td>\n",
       "      <td>0.003891</td>\n",
       "      <td>88.0</td>\n",
       "      <td>0.004287</td>\n",
       "      <td>-0.068781</td>\n",
       "      <td>244.0</td>\n",
       "      <td>-0.016875</td>\n",
       "      <td>1.167735</td>\n",
       "      <td>43.0</td>\n",
       "      <td>0.708720</td>\n",
       "      <td>0.780527</td>\n",
       "      <td>0.862017</td>\n",
       "      <td>0.919622</td>\n",
       "      <td>0.994468</td>\n",
       "      <td>1.065228</td>\n",
       "      <td>1.141907</td>\n",
       "      <td>1.270604</td>\n",
       "      <td>1.398097</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-08</td>\n",
       "      <td>3.0</td>\n",
       "      <td>17.28</td>\n",
       "      <td>31.0</td>\n",
       "      <td>16.14</td>\n",
       "      <td>71852835.0</td>\n",
       "      <td>1.172085e+09</td>\n",
       "      <td>1.728437e+10</td>\n",
       "      <td>2.504681e+10</td>\n",
       "      <td>21.0</td>\n",
       "      <td>-0.064890</td>\n",
       "      <td>-0.064890</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-08-31</td>\n",
       "      <td>0.003891</td>\n",
       "      <td>89.0</td>\n",
       "      <td>-0.068781</td>\n",
       "      <td>-0.026815</td>\n",
       "      <td>244.0</td>\n",
       "      <td>-0.125304</td>\n",
       "      <td>1.152495</td>\n",
       "      <td>44.0</td>\n",
       "      <td>0.708167</td>\n",
       "      <td>0.774056</td>\n",
       "      <td>0.849154</td>\n",
       "      <td>0.922411</td>\n",
       "      <td>0.989177</td>\n",
       "      <td>1.068256</td>\n",
       "      <td>1.151609</td>\n",
       "      <td>1.247518</td>\n",
       "      <td>1.387920</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>502361</th>\n",
       "      <td>605507</td>\n",
       "      <td>2024-12</td>\n",
       "      <td>2.0</td>\n",
       "      <td>22.07</td>\n",
       "      <td>31.0</td>\n",
       "      <td>20.83</td>\n",
       "      <td>61892279.0</td>\n",
       "      <td>1.307565e+09</td>\n",
       "      <td>6.284873e+09</td>\n",
       "      <td>1.164029e+10</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.047989</td>\n",
       "      <td>-0.047989</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2023-02-02</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>41.0</td>\n",
       "      <td>-0.049107</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.003797</td>\n",
       "      <td>0.604456</td>\n",
       "      <td>40.0</td>\n",
       "      <td>0.609035</td>\n",
       "      <td>0.742393</td>\n",
       "      <td>0.841648</td>\n",
       "      <td>0.923380</td>\n",
       "      <td>1.017028</td>\n",
       "      <td>1.108646</td>\n",
       "      <td>1.208609</td>\n",
       "      <td>1.329028</td>\n",
       "      <td>1.529482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>502362</th>\n",
       "      <td>605577</td>\n",
       "      <td>2024-12</td>\n",
       "      <td>2.0</td>\n",
       "      <td>16.93</td>\n",
       "      <td>31.0</td>\n",
       "      <td>14.53</td>\n",
       "      <td>228878763.0</td>\n",
       "      <td>3.862518e+09</td>\n",
       "      <td>6.457778e+09</td>\n",
       "      <td>6.457778e+09</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.135119</td>\n",
       "      <td>-0.135119</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-09-30</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>41.0</td>\n",
       "      <td>-0.136237</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.003797</td>\n",
       "      <td>0.764475</td>\n",
       "      <td>40.0</td>\n",
       "      <td>0.609035</td>\n",
       "      <td>0.742393</td>\n",
       "      <td>0.841648</td>\n",
       "      <td>0.923380</td>\n",
       "      <td>1.017028</td>\n",
       "      <td>1.108646</td>\n",
       "      <td>1.208609</td>\n",
       "      <td>1.329028</td>\n",
       "      <td>1.529482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>502363</th>\n",
       "      <td>605580</td>\n",
       "      <td>2024-12</td>\n",
       "      <td>2.0</td>\n",
       "      <td>9.88</td>\n",
       "      <td>31.0</td>\n",
       "      <td>11.08</td>\n",
       "      <td>154703585.0</td>\n",
       "      <td>1.623849e+09</td>\n",
       "      <td>3.102400e+09</td>\n",
       "      <td>3.102400e+09</td>\n",
       "      <td>22.0</td>\n",
       "      <td>0.132924</td>\n",
       "      <td>0.132924</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-08-26</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>41.0</td>\n",
       "      <td>0.131806</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.003797</td>\n",
       "      <td>0.785369</td>\n",
       "      <td>40.0</td>\n",
       "      <td>0.609035</td>\n",
       "      <td>0.742393</td>\n",
       "      <td>0.841648</td>\n",
       "      <td>0.923380</td>\n",
       "      <td>1.017028</td>\n",
       "      <td>1.108646</td>\n",
       "      <td>1.208609</td>\n",
       "      <td>1.329028</td>\n",
       "      <td>1.529482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>502364</th>\n",
       "      <td>605588</td>\n",
       "      <td>2024-12</td>\n",
       "      <td>2.0</td>\n",
       "      <td>47.96</td>\n",
       "      <td>31.0</td>\n",
       "      <td>40.76</td>\n",
       "      <td>41273627.0</td>\n",
       "      <td>1.873835e+09</td>\n",
       "      <td>2.979538e+09</td>\n",
       "      <td>2.995151e+09</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.150302</td>\n",
       "      <td>-0.150302</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-12-17</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>41.0</td>\n",
       "      <td>-0.151420</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.003797</td>\n",
       "      <td>0.976478</td>\n",
       "      <td>40.0</td>\n",
       "      <td>0.609035</td>\n",
       "      <td>0.742393</td>\n",
       "      <td>0.841648</td>\n",
       "      <td>0.923380</td>\n",
       "      <td>1.017028</td>\n",
       "      <td>1.108646</td>\n",
       "      <td>1.208609</td>\n",
       "      <td>1.329028</td>\n",
       "      <td>1.529482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>502365</th>\n",
       "      <td>605589</td>\n",
       "      <td>2024-12</td>\n",
       "      <td>2.0</td>\n",
       "      <td>25.08</td>\n",
       "      <td>31.0</td>\n",
       "      <td>23.58</td>\n",
       "      <td>210979113.0</td>\n",
       "      <td>5.061180e+09</td>\n",
       "      <td>1.835846e+10</td>\n",
       "      <td>1.995943e+10</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.023603</td>\n",
       "      <td>-0.023602</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-11-07</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>41.0</td>\n",
       "      <td>-0.024721</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.003797</td>\n",
       "      <td>1.717052</td>\n",
       "      <td>40.0</td>\n",
       "      <td>0.609035</td>\n",
       "      <td>0.742393</td>\n",
       "      <td>0.841648</td>\n",
       "      <td>0.923380</td>\n",
       "      <td>1.017028</td>\n",
       "      <td>1.108646</td>\n",
       "      <td>1.208609</td>\n",
       "      <td>1.329028</td>\n",
       "      <td>1.529482</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>502366 rows × 35 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 45
  },
  {
   "cell_type": "code",
   "id": "a1a6cce0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:08:38.456971Z",
     "start_time": "2025-11-18T06:08:38.224677Z"
    }
   },
   "source": [
    "portfolio['sort'] = np.where(\n",
    "    portfolio['beta'] <= portfolio['one'], 'P1',\n",
    "    np.where(\n",
    "        portfolio['beta'] <= portfolio['two'], 'P2',\n",
    "        np.where(\n",
    "            portfolio['beta'] <= portfolio['three'], 'P3',\n",
    "            np.where(\n",
    "                portfolio['beta'] <= portfolio['four'], 'P4',\n",
    "                np.where(\n",
    "                    portfolio['beta'] <= portfolio['five'], 'P5',\n",
    "                    np.where(\n",
    "                        portfolio['beta'] <= portfolio['six'], 'P6',\n",
    "                        np.where(\n",
    "                            portfolio['beta'] <= portfolio['seven'], 'P7',\n",
    "                            np.where(\n",
    "                                portfolio['beta'] <= portfolio['eight'], 'P8',\n",
    "                                np.where(\n",
    "                                    portfolio['beta'] <= portfolio['nine'],\n",
    "                                    'P9', 'Pmax')))))))))\n",
    "portfolio = portfolio.dropna(subset=['floatingvalue','next_ret','beta'])\n",
    "portfolio\n"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         Stkcd   Trdmnt  Opndt  Mopnprc  Clsdt  Mclsprc     Mnshrtrd  \\\n",
       "0       000001  1998-04    1.0    21.50   30.0    20.86  135781919.0   \n",
       "1       000001  1998-05    4.0    20.90   29.0    20.07  108329870.0   \n",
       "2       000001  1998-06    1.0    20.10   30.0    17.12   91965412.0   \n",
       "3       000001  1998-07    1.0    17.58   31.0    17.26  102722557.0   \n",
       "4       000001  1998-08    3.0    17.28   31.0    16.14   71852835.0   \n",
       "...        ...      ...    ...      ...    ...      ...          ...   \n",
       "502355  605500  2024-07    1.0     6.16   31.0     6.40   32852013.0   \n",
       "502356  605500  2024-08    1.0     6.40   30.0     6.18   24283030.0   \n",
       "502357  605500  2024-09    2.0     6.17   30.0     7.13   27875613.0   \n",
       "502358  605500  2024-10    8.0     7.83   31.0     7.05   49883893.0   \n",
       "502359  605500  2024-11    1.0     7.06   29.0     8.03   66866137.0   \n",
       "\n",
       "            Mnvaltrd  floatingvalue    totalvalue  Ndaytrd    Return  \\\n",
       "0       2.931748e+09   2.233903e+10  3.237153e+10     21.0 -0.032916   \n",
       "1       2.208166e+09   2.149301e+10  3.114557e+10     20.0 -0.037872   \n",
       "2       1.746011e+09   1.833385e+10  2.656762e+10     22.0 -0.146986   \n",
       "3       1.740172e+09   1.848378e+10  2.678488e+10     23.0  0.008178   \n",
       "4       1.172085e+09   1.728437e+10  2.504681e+10     21.0 -0.064890   \n",
       "...              ...            ...           ...      ...       ...   \n",
       "502355  2.035688e+08   2.652160e+09  2.652160e+09     23.0  0.067517   \n",
       "502356  1.500448e+08   2.560992e+09  2.560992e+09     22.0 -0.034375   \n",
       "502357  1.777782e+08   2.954672e+09  2.954672e+09     19.0  0.153722   \n",
       "502358  3.539763e+08   2.921520e+09  2.921520e+09     18.0 -0.011220   \n",
       "502359  5.037812e+08   3.327632e+09  3.327632e+09     21.0  0.139007   \n",
       "\n",
       "          Mretnd  Markettype    Capchgdt  Ahshrtrd_M  Ahvaltrd_M      month  \\\n",
       "0      -0.032916         4.0  1997-08-25         NaN         NaN 1998-04-30   \n",
       "1      -0.037872         4.0  1997-08-25         NaN         NaN 1998-05-31   \n",
       "2      -0.146986         4.0  1997-08-25         NaN         NaN 1998-06-30   \n",
       "3       0.008178         4.0  1997-08-25         NaN         NaN 1998-07-31   \n",
       "4      -0.064890         4.0  1997-08-25         NaN         NaN 1998-08-31   \n",
       "...          ...         ...         ...         ...         ...        ...   \n",
       "502355  0.065041         1.0  2024-06-24         0.0         0.0 2024-07-31   \n",
       "502356 -0.034375         1.0  2024-06-24         0.0         0.0 2024-08-31   \n",
       "502357  0.153722         1.0  2024-06-24         0.0         0.0 2024-09-30   \n",
       "502358 -0.011220         1.0  2024-06-24         0.0         0.0 2024-10-31   \n",
       "502359  0.139007         1.0  2024-06-24         0.0         0.0 2024-11-30   \n",
       "\n",
       "         rfmonth  list_month       ret  next_ret  Cumsum_tradingday       MKT  \\\n",
       "0       0.004249        85.0 -0.037165 -0.042121              235.0  0.085311   \n",
       "1       0.004249        86.0 -0.042121 -0.151235              235.0  0.050219   \n",
       "2       0.004249        87.0 -0.151235  0.004287              242.0 -0.066011   \n",
       "3       0.003891        88.0  0.004287 -0.068781              244.0 -0.016875   \n",
       "4       0.003891        89.0 -0.068781 -0.026815              244.0 -0.125304   \n",
       "...          ...         ...       ...       ...                ...       ...   \n",
       "502355  0.001184        44.0  0.066333 -0.035493              243.0 -0.000624   \n",
       "502356  0.001118        45.0 -0.035493  0.152604              242.0 -0.032586   \n",
       "502357  0.001118        46.0  0.152604 -0.012338              241.0  0.194874   \n",
       "502358  0.001118        47.0 -0.012338  0.137889              242.0 -0.004527   \n",
       "502359  0.001118        48.0  0.137889 -0.134368              241.0  0.008606   \n",
       "\n",
       "            beta  data_count       one       two     three      four  \\\n",
       "0       1.155400        40.0  0.707349  0.781328  0.847070  0.918619   \n",
       "1       1.148875        41.0  0.710665  0.786197  0.852553  0.921351   \n",
       "2       1.167744        42.0  0.720040  0.782919  0.868701  0.920163   \n",
       "3       1.167735        43.0  0.708720  0.780527  0.862017  0.919622   \n",
       "4       1.152495        44.0  0.708167  0.774056  0.849154  0.922411   \n",
       "...          ...         ...       ...       ...       ...       ...   \n",
       "502355  0.796606        43.0  0.523361  0.692383  0.806922  0.904072   \n",
       "502356  0.798576        44.0  0.527212  0.690668  0.808727  0.903639   \n",
       "502357  0.800304        45.0  0.613942  0.747197  0.844887  0.929128   \n",
       "502358  0.800487        46.0  0.609603  0.741257  0.840805  0.923708   \n",
       "502359  0.809865        47.0  0.611609  0.744125  0.844421  0.925974   \n",
       "\n",
       "            five       six     seven     eight      nine sort  \n",
       "0       0.986271  1.053890  1.147653  1.263057  1.403121   P8  \n",
       "1       0.992781  1.057841  1.143836  1.276886  1.410021   P8  \n",
       "2       0.991961  1.060550  1.148936  1.277518  1.404875   P8  \n",
       "3       0.994468  1.065228  1.141907  1.270604  1.398097   P8  \n",
       "4       0.989177  1.068256  1.151609  1.247518  1.387920   P8  \n",
       "...          ...       ...       ...       ...       ...  ...  \n",
       "502355  1.002450  1.105938  1.233599  1.373471  1.608908   P3  \n",
       "502356  1.005142  1.103606  1.236126  1.375513  1.610912   P3  \n",
       "502357  1.020618  1.112952  1.212442  1.330217  1.528623   P3  \n",
       "502358  1.015869  1.106596  1.205720  1.327996  1.526917   P3  \n",
       "502359  1.016210  1.107136  1.206734  1.328866  1.524422   P3  \n",
       "\n",
       "[496890 rows x 36 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stkcd</th>\n",
       "      <th>Trdmnt</th>\n",
       "      <th>Opndt</th>\n",
       "      <th>Mopnprc</th>\n",
       "      <th>Clsdt</th>\n",
       "      <th>Mclsprc</th>\n",
       "      <th>Mnshrtrd</th>\n",
       "      <th>Mnvaltrd</th>\n",
       "      <th>floatingvalue</th>\n",
       "      <th>totalvalue</th>\n",
       "      <th>Ndaytrd</th>\n",
       "      <th>Return</th>\n",
       "      <th>Mretnd</th>\n",
       "      <th>Markettype</th>\n",
       "      <th>Capchgdt</th>\n",
       "      <th>Ahshrtrd_M</th>\n",
       "      <th>Ahvaltrd_M</th>\n",
       "      <th>month</th>\n",
       "      <th>rfmonth</th>\n",
       "      <th>list_month</th>\n",
       "      <th>ret</th>\n",
       "      <th>next_ret</th>\n",
       "      <th>Cumsum_tradingday</th>\n",
       "      <th>MKT</th>\n",
       "      <th>beta</th>\n",
       "      <th>data_count</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "      <th>seven</th>\n",
       "      <th>eight</th>\n",
       "      <th>nine</th>\n",
       "      <th>sort</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-04</td>\n",
       "      <td>1.0</td>\n",
       "      <td>21.50</td>\n",
       "      <td>30.0</td>\n",
       "      <td>20.86</td>\n",
       "      <td>135781919.0</td>\n",
       "      <td>2.931748e+09</td>\n",
       "      <td>2.233903e+10</td>\n",
       "      <td>3.237153e+10</td>\n",
       "      <td>21.0</td>\n",
       "      <td>-0.032916</td>\n",
       "      <td>-0.032916</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-04-30</td>\n",
       "      <td>0.004249</td>\n",
       "      <td>85.0</td>\n",
       "      <td>-0.037165</td>\n",
       "      <td>-0.042121</td>\n",
       "      <td>235.0</td>\n",
       "      <td>0.085311</td>\n",
       "      <td>1.155400</td>\n",
       "      <td>40.0</td>\n",
       "      <td>0.707349</td>\n",
       "      <td>0.781328</td>\n",
       "      <td>0.847070</td>\n",
       "      <td>0.918619</td>\n",
       "      <td>0.986271</td>\n",
       "      <td>1.053890</td>\n",
       "      <td>1.147653</td>\n",
       "      <td>1.263057</td>\n",
       "      <td>1.403121</td>\n",
       "      <td>P8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-05</td>\n",
       "      <td>4.0</td>\n",
       "      <td>20.90</td>\n",
       "      <td>29.0</td>\n",
       "      <td>20.07</td>\n",
       "      <td>108329870.0</td>\n",
       "      <td>2.208166e+09</td>\n",
       "      <td>2.149301e+10</td>\n",
       "      <td>3.114557e+10</td>\n",
       "      <td>20.0</td>\n",
       "      <td>-0.037872</td>\n",
       "      <td>-0.037872</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-05-31</td>\n",
       "      <td>0.004249</td>\n",
       "      <td>86.0</td>\n",
       "      <td>-0.042121</td>\n",
       "      <td>-0.151235</td>\n",
       "      <td>235.0</td>\n",
       "      <td>0.050219</td>\n",
       "      <td>1.148875</td>\n",
       "      <td>41.0</td>\n",
       "      <td>0.710665</td>\n",
       "      <td>0.786197</td>\n",
       "      <td>0.852553</td>\n",
       "      <td>0.921351</td>\n",
       "      <td>0.992781</td>\n",
       "      <td>1.057841</td>\n",
       "      <td>1.143836</td>\n",
       "      <td>1.276886</td>\n",
       "      <td>1.410021</td>\n",
       "      <td>P8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-06</td>\n",
       "      <td>1.0</td>\n",
       "      <td>20.10</td>\n",
       "      <td>30.0</td>\n",
       "      <td>17.12</td>\n",
       "      <td>91965412.0</td>\n",
       "      <td>1.746011e+09</td>\n",
       "      <td>1.833385e+10</td>\n",
       "      <td>2.656762e+10</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.146986</td>\n",
       "      <td>-0.146986</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-06-30</td>\n",
       "      <td>0.004249</td>\n",
       "      <td>87.0</td>\n",
       "      <td>-0.151235</td>\n",
       "      <td>0.004287</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.066011</td>\n",
       "      <td>1.167744</td>\n",
       "      <td>42.0</td>\n",
       "      <td>0.720040</td>\n",
       "      <td>0.782919</td>\n",
       "      <td>0.868701</td>\n",
       "      <td>0.920163</td>\n",
       "      <td>0.991961</td>\n",
       "      <td>1.060550</td>\n",
       "      <td>1.148936</td>\n",
       "      <td>1.277518</td>\n",
       "      <td>1.404875</td>\n",
       "      <td>P8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>17.58</td>\n",
       "      <td>31.0</td>\n",
       "      <td>17.26</td>\n",
       "      <td>102722557.0</td>\n",
       "      <td>1.740172e+09</td>\n",
       "      <td>1.848378e+10</td>\n",
       "      <td>2.678488e+10</td>\n",
       "      <td>23.0</td>\n",
       "      <td>0.008178</td>\n",
       "      <td>0.008178</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-07-31</td>\n",
       "      <td>0.003891</td>\n",
       "      <td>88.0</td>\n",
       "      <td>0.004287</td>\n",
       "      <td>-0.068781</td>\n",
       "      <td>244.0</td>\n",
       "      <td>-0.016875</td>\n",
       "      <td>1.167735</td>\n",
       "      <td>43.0</td>\n",
       "      <td>0.708720</td>\n",
       "      <td>0.780527</td>\n",
       "      <td>0.862017</td>\n",
       "      <td>0.919622</td>\n",
       "      <td>0.994468</td>\n",
       "      <td>1.065228</td>\n",
       "      <td>1.141907</td>\n",
       "      <td>1.270604</td>\n",
       "      <td>1.398097</td>\n",
       "      <td>P8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000001</td>\n",
       "      <td>1998-08</td>\n",
       "      <td>3.0</td>\n",
       "      <td>17.28</td>\n",
       "      <td>31.0</td>\n",
       "      <td>16.14</td>\n",
       "      <td>71852835.0</td>\n",
       "      <td>1.172085e+09</td>\n",
       "      <td>1.728437e+10</td>\n",
       "      <td>2.504681e+10</td>\n",
       "      <td>21.0</td>\n",
       "      <td>-0.064890</td>\n",
       "      <td>-0.064890</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1998-08-31</td>\n",
       "      <td>0.003891</td>\n",
       "      <td>89.0</td>\n",
       "      <td>-0.068781</td>\n",
       "      <td>-0.026815</td>\n",
       "      <td>244.0</td>\n",
       "      <td>-0.125304</td>\n",
       "      <td>1.152495</td>\n",
       "      <td>44.0</td>\n",
       "      <td>0.708167</td>\n",
       "      <td>0.774056</td>\n",
       "      <td>0.849154</td>\n",
       "      <td>0.922411</td>\n",
       "      <td>0.989177</td>\n",
       "      <td>1.068256</td>\n",
       "      <td>1.151609</td>\n",
       "      <td>1.247518</td>\n",
       "      <td>1.387920</td>\n",
       "      <td>P8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>502355</th>\n",
       "      <td>605500</td>\n",
       "      <td>2024-07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6.16</td>\n",
       "      <td>31.0</td>\n",
       "      <td>6.40</td>\n",
       "      <td>32852013.0</td>\n",
       "      <td>2.035688e+08</td>\n",
       "      <td>2.652160e+09</td>\n",
       "      <td>2.652160e+09</td>\n",
       "      <td>23.0</td>\n",
       "      <td>0.067517</td>\n",
       "      <td>0.065041</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-06-24</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-07-31</td>\n",
       "      <td>0.001184</td>\n",
       "      <td>44.0</td>\n",
       "      <td>0.066333</td>\n",
       "      <td>-0.035493</td>\n",
       "      <td>243.0</td>\n",
       "      <td>-0.000624</td>\n",
       "      <td>0.796606</td>\n",
       "      <td>43.0</td>\n",
       "      <td>0.523361</td>\n",
       "      <td>0.692383</td>\n",
       "      <td>0.806922</td>\n",
       "      <td>0.904072</td>\n",
       "      <td>1.002450</td>\n",
       "      <td>1.105938</td>\n",
       "      <td>1.233599</td>\n",
       "      <td>1.373471</td>\n",
       "      <td>1.608908</td>\n",
       "      <td>P3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>502356</th>\n",
       "      <td>605500</td>\n",
       "      <td>2024-08</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6.40</td>\n",
       "      <td>30.0</td>\n",
       "      <td>6.18</td>\n",
       "      <td>24283030.0</td>\n",
       "      <td>1.500448e+08</td>\n",
       "      <td>2.560992e+09</td>\n",
       "      <td>2.560992e+09</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.034375</td>\n",
       "      <td>-0.034375</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-06-24</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-08-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>45.0</td>\n",
       "      <td>-0.035493</td>\n",
       "      <td>0.152604</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.032586</td>\n",
       "      <td>0.798576</td>\n",
       "      <td>44.0</td>\n",
       "      <td>0.527212</td>\n",
       "      <td>0.690668</td>\n",
       "      <td>0.808727</td>\n",
       "      <td>0.903639</td>\n",
       "      <td>1.005142</td>\n",
       "      <td>1.103606</td>\n",
       "      <td>1.236126</td>\n",
       "      <td>1.375513</td>\n",
       "      <td>1.610912</td>\n",
       "      <td>P3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>502357</th>\n",
       "      <td>605500</td>\n",
       "      <td>2024-09</td>\n",
       "      <td>2.0</td>\n",
       "      <td>6.17</td>\n",
       "      <td>30.0</td>\n",
       "      <td>7.13</td>\n",
       "      <td>27875613.0</td>\n",
       "      <td>1.777782e+08</td>\n",
       "      <td>2.954672e+09</td>\n",
       "      <td>2.954672e+09</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0.153722</td>\n",
       "      <td>0.153722</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-06-24</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-09-30</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>46.0</td>\n",
       "      <td>0.152604</td>\n",
       "      <td>-0.012338</td>\n",
       "      <td>241.0</td>\n",
       "      <td>0.194874</td>\n",
       "      <td>0.800304</td>\n",
       "      <td>45.0</td>\n",
       "      <td>0.613942</td>\n",
       "      <td>0.747197</td>\n",
       "      <td>0.844887</td>\n",
       "      <td>0.929128</td>\n",
       "      <td>1.020618</td>\n",
       "      <td>1.112952</td>\n",
       "      <td>1.212442</td>\n",
       "      <td>1.330217</td>\n",
       "      <td>1.528623</td>\n",
       "      <td>P3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>502358</th>\n",
       "      <td>605500</td>\n",
       "      <td>2024-10</td>\n",
       "      <td>8.0</td>\n",
       "      <td>7.83</td>\n",
       "      <td>31.0</td>\n",
       "      <td>7.05</td>\n",
       "      <td>49883893.0</td>\n",
       "      <td>3.539763e+08</td>\n",
       "      <td>2.921520e+09</td>\n",
       "      <td>2.921520e+09</td>\n",
       "      <td>18.0</td>\n",
       "      <td>-0.011220</td>\n",
       "      <td>-0.011220</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-06-24</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-10-31</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>47.0</td>\n",
       "      <td>-0.012338</td>\n",
       "      <td>0.137889</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.004527</td>\n",
       "      <td>0.800487</td>\n",
       "      <td>46.0</td>\n",
       "      <td>0.609603</td>\n",
       "      <td>0.741257</td>\n",
       "      <td>0.840805</td>\n",
       "      <td>0.923708</td>\n",
       "      <td>1.015869</td>\n",
       "      <td>1.106596</td>\n",
       "      <td>1.205720</td>\n",
       "      <td>1.327996</td>\n",
       "      <td>1.526917</td>\n",
       "      <td>P3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>502359</th>\n",
       "      <td>605500</td>\n",
       "      <td>2024-11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>7.06</td>\n",
       "      <td>29.0</td>\n",
       "      <td>8.03</td>\n",
       "      <td>66866137.0</td>\n",
       "      <td>5.037812e+08</td>\n",
       "      <td>3.327632e+09</td>\n",
       "      <td>3.327632e+09</td>\n",
       "      <td>21.0</td>\n",
       "      <td>0.139007</td>\n",
       "      <td>0.139007</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-06-24</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024-11-30</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>48.0</td>\n",
       "      <td>0.137889</td>\n",
       "      <td>-0.134368</td>\n",
       "      <td>241.0</td>\n",
       "      <td>0.008606</td>\n",
       "      <td>0.809865</td>\n",
       "      <td>47.0</td>\n",
       "      <td>0.611609</td>\n",
       "      <td>0.744125</td>\n",
       "      <td>0.844421</td>\n",
       "      <td>0.925974</td>\n",
       "      <td>1.016210</td>\n",
       "      <td>1.107136</td>\n",
       "      <td>1.206734</td>\n",
       "      <td>1.328866</td>\n",
       "      <td>1.524422</td>\n",
       "      <td>P3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>496890 rows × 36 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 46
  },
  {
   "cell_type": "code",
   "id": "2f71289c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:08:41.694683Z",
     "start_time": "2025-11-18T06:08:40.901381Z"
    }
   },
   "source": [
    "portfolio = portfolio.dropna(subset=['next_ret','floatingvalue','beta'])\n",
    "portfolio_beta =  pd.DataFrame(\n",
    "    portfolio.groupby(['month','sort']).apply(lambda x: np.average(x['next_ret'],weights = x['floatingvalue']),include_groups=False))\n",
    "portfolio_beta"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                        0\n",
       "month      sort          \n",
       "1998-04-30 P1    0.073639\n",
       "           P2    0.073060\n",
       "           P3    0.059682\n",
       "           P4    0.126568\n",
       "           P5    0.060105\n",
       "...                   ...\n",
       "2024-11-30 P6   -0.002935\n",
       "           P7   -0.022196\n",
       "           P8   -0.021325\n",
       "           P9   -0.036926\n",
       "           Pmax -0.050939\n",
       "\n",
       "[3200 rows x 1 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>month</th>\n",
       "      <th>sort</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">1998-04-30</th>\n",
       "      <th>P1</th>\n",
       "      <td>0.073639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>P2</th>\n",
       "      <td>0.073060</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>P3</th>\n",
       "      <td>0.059682</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>P4</th>\n",
       "      <td>0.126568</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>P5</th>\n",
       "      <td>0.060105</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2024-11-30</th>\n",
       "      <th>P6</th>\n",
       "      <td>-0.002935</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>P7</th>\n",
       "      <td>-0.022196</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>P8</th>\n",
       "      <td>-0.021325</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>P9</th>\n",
       "      <td>-0.036926</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pmax</th>\n",
       "      <td>-0.050939</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3200 rows × 1 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 47
  },
  {
   "cell_type": "code",
   "id": "e354668a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:08:43.347160Z",
     "start_time": "2025-11-18T06:08:43.335957Z"
    }
   },
   "source": [
    "portfolio_beta = portfolio_beta.reset_index()\n",
    "portfolio_beta.columns = ['month', 'sort', 'p']\n",
    "portfolio_beta['month'] = portfolio_beta['month'] + MonthEnd(1)\n",
    "portfolio_beta"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          month  sort         p\n",
       "0    1998-05-31    P1  0.073639\n",
       "1    1998-05-31    P2  0.073060\n",
       "2    1998-05-31    P3  0.059682\n",
       "3    1998-05-31    P4  0.126568\n",
       "4    1998-05-31    P5  0.060105\n",
       "...         ...   ...       ...\n",
       "3195 2024-12-31    P6 -0.002935\n",
       "3196 2024-12-31    P7 -0.022196\n",
       "3197 2024-12-31    P8 -0.021325\n",
       "3198 2024-12-31    P9 -0.036926\n",
       "3199 2024-12-31  Pmax -0.050939\n",
       "\n",
       "[3200 rows x 3 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>month</th>\n",
       "      <th>sort</th>\n",
       "      <th>p</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1998-05-31</td>\n",
       "      <td>P1</td>\n",
       "      <td>0.073639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1998-05-31</td>\n",
       "      <td>P2</td>\n",
       "      <td>0.073060</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1998-05-31</td>\n",
       "      <td>P3</td>\n",
       "      <td>0.059682</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1998-05-31</td>\n",
       "      <td>P4</td>\n",
       "      <td>0.126568</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1998-05-31</td>\n",
       "      <td>P5</td>\n",
       "      <td>0.060105</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3195</th>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>P6</td>\n",
       "      <td>-0.002935</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3196</th>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>P7</td>\n",
       "      <td>-0.022196</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3197</th>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>P8</td>\n",
       "      <td>-0.021325</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3198</th>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>P9</td>\n",
       "      <td>-0.036926</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3199</th>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>Pmax</td>\n",
       "      <td>-0.050939</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3200 rows × 3 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 48
  },
  {
   "cell_type": "code",
   "id": "601ea9c8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:08:45.103643Z",
     "start_time": "2025-11-18T06:08:45.082849Z"
    }
   },
   "source": [
    "portfolio_beta = portfolio_beta.pivot_table(index='month',\n",
    "                                            columns='sort',\n",
    "                                            values='p')\n",
    "portfolio_beta['My_portfolio'] = portfolio_beta['Pmax'] - portfolio_beta['P1']\n",
    "portfolio_beta"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "sort              P1        P2        P3        P4        P5        P6  \\\n",
       "month                                                                    \n",
       "1998-05-31  0.073639  0.073060  0.059682  0.126568  0.060105  0.110886   \n",
       "1998-06-30 -0.068016 -0.083137 -0.033254 -0.073349 -0.042640 -0.083524   \n",
       "1998-07-31  0.022387  0.002304 -0.010380 -0.030658 -0.022810 -0.021796   \n",
       "1998-08-31 -0.104332 -0.088575 -0.109517 -0.094662 -0.144915 -0.150273   \n",
       "1998-09-30  0.062278  0.071437  0.070417  0.035619  0.073661  0.034641   \n",
       "...              ...       ...       ...       ...       ...       ...   \n",
       "2024-08-31 -0.009478 -0.040298 -0.038603 -0.042049 -0.051192 -0.015255   \n",
       "2024-09-30  0.083155  0.182333  0.161097  0.225707  0.206607  0.217508   \n",
       "2024-10-31 -0.052130 -0.031471 -0.012259  0.032790 -0.008649 -0.028356   \n",
       "2024-11-30  0.008144  0.004305  0.014320  0.043201  0.001602 -0.011967   \n",
       "2024-12-31  0.064299  0.001241 -0.009634 -0.001426  0.002722 -0.002935   \n",
       "\n",
       "sort              P7        P8        P9      Pmax  My_portfolio  \n",
       "month                                                             \n",
       "1998-05-31  0.070398  0.014426  0.066955  0.065713     -0.007925  \n",
       "1998-06-30 -0.059467 -0.133064 -0.125269 -0.091285     -0.023269  \n",
       "1998-07-31 -0.021273 -0.005191  0.017707  0.013602     -0.008785  \n",
       "1998-08-31 -0.133799 -0.111460 -0.133402 -0.137561     -0.033229  \n",
       "1998-09-30  0.107009  0.023006  0.067436  0.085260      0.022982  \n",
       "...              ...       ...       ...       ...           ...  \n",
       "2024-08-31 -0.039640 -0.023992 -0.043447 -0.056096     -0.046618  \n",
       "2024-09-30  0.247355  0.228438  0.225740  0.238674      0.155519  \n",
       "2024-10-31  0.007237  0.005666 -0.009967  0.042866      0.094996  \n",
       "2024-11-30  0.004125  0.002910  0.016998  0.001745     -0.006399  \n",
       "2024-12-31 -0.022196 -0.021325 -0.036926 -0.050939     -0.115238  \n",
       "\n",
       "[320 rows x 11 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>sort</th>\n",
       "      <th>P1</th>\n",
       "      <th>P2</th>\n",
       "      <th>P3</th>\n",
       "      <th>P4</th>\n",
       "      <th>P5</th>\n",
       "      <th>P6</th>\n",
       "      <th>P7</th>\n",
       "      <th>P8</th>\n",
       "      <th>P9</th>\n",
       "      <th>Pmax</th>\n",
       "      <th>My_portfolio</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>month</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1998-05-31</th>\n",
       "      <td>0.073639</td>\n",
       "      <td>0.073060</td>\n",
       "      <td>0.059682</td>\n",
       "      <td>0.126568</td>\n",
       "      <td>0.060105</td>\n",
       "      <td>0.110886</td>\n",
       "      <td>0.070398</td>\n",
       "      <td>0.014426</td>\n",
       "      <td>0.066955</td>\n",
       "      <td>0.065713</td>\n",
       "      <td>-0.007925</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06-30</th>\n",
       "      <td>-0.068016</td>\n",
       "      <td>-0.083137</td>\n",
       "      <td>-0.033254</td>\n",
       "      <td>-0.073349</td>\n",
       "      <td>-0.042640</td>\n",
       "      <td>-0.083524</td>\n",
       "      <td>-0.059467</td>\n",
       "      <td>-0.133064</td>\n",
       "      <td>-0.125269</td>\n",
       "      <td>-0.091285</td>\n",
       "      <td>-0.023269</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-07-31</th>\n",
       "      <td>0.022387</td>\n",
       "      <td>0.002304</td>\n",
       "      <td>-0.010380</td>\n",
       "      <td>-0.030658</td>\n",
       "      <td>-0.022810</td>\n",
       "      <td>-0.021796</td>\n",
       "      <td>-0.021273</td>\n",
       "      <td>-0.005191</td>\n",
       "      <td>0.017707</td>\n",
       "      <td>0.013602</td>\n",
       "      <td>-0.008785</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-08-31</th>\n",
       "      <td>-0.104332</td>\n",
       "      <td>-0.088575</td>\n",
       "      <td>-0.109517</td>\n",
       "      <td>-0.094662</td>\n",
       "      <td>-0.144915</td>\n",
       "      <td>-0.150273</td>\n",
       "      <td>-0.133799</td>\n",
       "      <td>-0.111460</td>\n",
       "      <td>-0.133402</td>\n",
       "      <td>-0.137561</td>\n",
       "      <td>-0.033229</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-09-30</th>\n",
       "      <td>0.062278</td>\n",
       "      <td>0.071437</td>\n",
       "      <td>0.070417</td>\n",
       "      <td>0.035619</td>\n",
       "      <td>0.073661</td>\n",
       "      <td>0.034641</td>\n",
       "      <td>0.107009</td>\n",
       "      <td>0.023006</td>\n",
       "      <td>0.067436</td>\n",
       "      <td>0.085260</td>\n",
       "      <td>0.022982</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-08-31</th>\n",
       "      <td>-0.009478</td>\n",
       "      <td>-0.040298</td>\n",
       "      <td>-0.038603</td>\n",
       "      <td>-0.042049</td>\n",
       "      <td>-0.051192</td>\n",
       "      <td>-0.015255</td>\n",
       "      <td>-0.039640</td>\n",
       "      <td>-0.023992</td>\n",
       "      <td>-0.043447</td>\n",
       "      <td>-0.056096</td>\n",
       "      <td>-0.046618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-30</th>\n",
       "      <td>0.083155</td>\n",
       "      <td>0.182333</td>\n",
       "      <td>0.161097</td>\n",
       "      <td>0.225707</td>\n",
       "      <td>0.206607</td>\n",
       "      <td>0.217508</td>\n",
       "      <td>0.247355</td>\n",
       "      <td>0.228438</td>\n",
       "      <td>0.225740</td>\n",
       "      <td>0.238674</td>\n",
       "      <td>0.155519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-10-31</th>\n",
       "      <td>-0.052130</td>\n",
       "      <td>-0.031471</td>\n",
       "      <td>-0.012259</td>\n",
       "      <td>0.032790</td>\n",
       "      <td>-0.008649</td>\n",
       "      <td>-0.028356</td>\n",
       "      <td>0.007237</td>\n",
       "      <td>0.005666</td>\n",
       "      <td>-0.009967</td>\n",
       "      <td>0.042866</td>\n",
       "      <td>0.094996</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-30</th>\n",
       "      <td>0.008144</td>\n",
       "      <td>0.004305</td>\n",
       "      <td>0.014320</td>\n",
       "      <td>0.043201</td>\n",
       "      <td>0.001602</td>\n",
       "      <td>-0.011967</td>\n",
       "      <td>0.004125</td>\n",
       "      <td>0.002910</td>\n",
       "      <td>0.016998</td>\n",
       "      <td>0.001745</td>\n",
       "      <td>-0.006399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>0.064299</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>-0.009634</td>\n",
       "      <td>-0.001426</td>\n",
       "      <td>0.002722</td>\n",
       "      <td>-0.002935</td>\n",
       "      <td>-0.022196</td>\n",
       "      <td>-0.021325</td>\n",
       "      <td>-0.036926</td>\n",
       "      <td>-0.050939</td>\n",
       "      <td>-0.115238</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>320 rows × 11 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 49
  },
  {
   "cell_type": "code",
   "id": "a20074c8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:08:47.200849Z",
     "start_time": "2025-11-18T06:08:47.146827Z"
    }
   },
   "source": [
    "portfolio_beta = portfolio_beta['1995-01':'2024-12']\n",
    "portfolio_beta"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "sort              P1        P2        P3        P4        P5        P6  \\\n",
       "month                                                                    \n",
       "1998-05-31  0.073639  0.073060  0.059682  0.126568  0.060105  0.110886   \n",
       "1998-06-30 -0.068016 -0.083137 -0.033254 -0.073349 -0.042640 -0.083524   \n",
       "1998-07-31  0.022387  0.002304 -0.010380 -0.030658 -0.022810 -0.021796   \n",
       "1998-08-31 -0.104332 -0.088575 -0.109517 -0.094662 -0.144915 -0.150273   \n",
       "1998-09-30  0.062278  0.071437  0.070417  0.035619  0.073661  0.034641   \n",
       "...              ...       ...       ...       ...       ...       ...   \n",
       "2024-08-31 -0.009478 -0.040298 -0.038603 -0.042049 -0.051192 -0.015255   \n",
       "2024-09-30  0.083155  0.182333  0.161097  0.225707  0.206607  0.217508   \n",
       "2024-10-31 -0.052130 -0.031471 -0.012259  0.032790 -0.008649 -0.028356   \n",
       "2024-11-30  0.008144  0.004305  0.014320  0.043201  0.001602 -0.011967   \n",
       "2024-12-31  0.064299  0.001241 -0.009634 -0.001426  0.002722 -0.002935   \n",
       "\n",
       "sort              P7        P8        P9      Pmax  My_portfolio  \n",
       "month                                                             \n",
       "1998-05-31  0.070398  0.014426  0.066955  0.065713     -0.007925  \n",
       "1998-06-30 -0.059467 -0.133064 -0.125269 -0.091285     -0.023269  \n",
       "1998-07-31 -0.021273 -0.005191  0.017707  0.013602     -0.008785  \n",
       "1998-08-31 -0.133799 -0.111460 -0.133402 -0.137561     -0.033229  \n",
       "1998-09-30  0.107009  0.023006  0.067436  0.085260      0.022982  \n",
       "...              ...       ...       ...       ...           ...  \n",
       "2024-08-31 -0.039640 -0.023992 -0.043447 -0.056096     -0.046618  \n",
       "2024-09-30  0.247355  0.228438  0.225740  0.238674      0.155519  \n",
       "2024-10-31  0.007237  0.005666 -0.009967  0.042866      0.094996  \n",
       "2024-11-30  0.004125  0.002910  0.016998  0.001745     -0.006399  \n",
       "2024-12-31 -0.022196 -0.021325 -0.036926 -0.050939     -0.115238  \n",
       "\n",
       "[320 rows x 11 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>sort</th>\n",
       "      <th>P1</th>\n",
       "      <th>P2</th>\n",
       "      <th>P3</th>\n",
       "      <th>P4</th>\n",
       "      <th>P5</th>\n",
       "      <th>P6</th>\n",
       "      <th>P7</th>\n",
       "      <th>P8</th>\n",
       "      <th>P9</th>\n",
       "      <th>Pmax</th>\n",
       "      <th>My_portfolio</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>month</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1998-05-31</th>\n",
       "      <td>0.073639</td>\n",
       "      <td>0.073060</td>\n",
       "      <td>0.059682</td>\n",
       "      <td>0.126568</td>\n",
       "      <td>0.060105</td>\n",
       "      <td>0.110886</td>\n",
       "      <td>0.070398</td>\n",
       "      <td>0.014426</td>\n",
       "      <td>0.066955</td>\n",
       "      <td>0.065713</td>\n",
       "      <td>-0.007925</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06-30</th>\n",
       "      <td>-0.068016</td>\n",
       "      <td>-0.083137</td>\n",
       "      <td>-0.033254</td>\n",
       "      <td>-0.073349</td>\n",
       "      <td>-0.042640</td>\n",
       "      <td>-0.083524</td>\n",
       "      <td>-0.059467</td>\n",
       "      <td>-0.133064</td>\n",
       "      <td>-0.125269</td>\n",
       "      <td>-0.091285</td>\n",
       "      <td>-0.023269</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-07-31</th>\n",
       "      <td>0.022387</td>\n",
       "      <td>0.002304</td>\n",
       "      <td>-0.010380</td>\n",
       "      <td>-0.030658</td>\n",
       "      <td>-0.022810</td>\n",
       "      <td>-0.021796</td>\n",
       "      <td>-0.021273</td>\n",
       "      <td>-0.005191</td>\n",
       "      <td>0.017707</td>\n",
       "      <td>0.013602</td>\n",
       "      <td>-0.008785</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-08-31</th>\n",
       "      <td>-0.104332</td>\n",
       "      <td>-0.088575</td>\n",
       "      <td>-0.109517</td>\n",
       "      <td>-0.094662</td>\n",
       "      <td>-0.144915</td>\n",
       "      <td>-0.150273</td>\n",
       "      <td>-0.133799</td>\n",
       "      <td>-0.111460</td>\n",
       "      <td>-0.133402</td>\n",
       "      <td>-0.137561</td>\n",
       "      <td>-0.033229</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-09-30</th>\n",
       "      <td>0.062278</td>\n",
       "      <td>0.071437</td>\n",
       "      <td>0.070417</td>\n",
       "      <td>0.035619</td>\n",
       "      <td>0.073661</td>\n",
       "      <td>0.034641</td>\n",
       "      <td>0.107009</td>\n",
       "      <td>0.023006</td>\n",
       "      <td>0.067436</td>\n",
       "      <td>0.085260</td>\n",
       "      <td>0.022982</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-08-31</th>\n",
       "      <td>-0.009478</td>\n",
       "      <td>-0.040298</td>\n",
       "      <td>-0.038603</td>\n",
       "      <td>-0.042049</td>\n",
       "      <td>-0.051192</td>\n",
       "      <td>-0.015255</td>\n",
       "      <td>-0.039640</td>\n",
       "      <td>-0.023992</td>\n",
       "      <td>-0.043447</td>\n",
       "      <td>-0.056096</td>\n",
       "      <td>-0.046618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-30</th>\n",
       "      <td>0.083155</td>\n",
       "      <td>0.182333</td>\n",
       "      <td>0.161097</td>\n",
       "      <td>0.225707</td>\n",
       "      <td>0.206607</td>\n",
       "      <td>0.217508</td>\n",
       "      <td>0.247355</td>\n",
       "      <td>0.228438</td>\n",
       "      <td>0.225740</td>\n",
       "      <td>0.238674</td>\n",
       "      <td>0.155519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-10-31</th>\n",
       "      <td>-0.052130</td>\n",
       "      <td>-0.031471</td>\n",
       "      <td>-0.012259</td>\n",
       "      <td>0.032790</td>\n",
       "      <td>-0.008649</td>\n",
       "      <td>-0.028356</td>\n",
       "      <td>0.007237</td>\n",
       "      <td>0.005666</td>\n",
       "      <td>-0.009967</td>\n",
       "      <td>0.042866</td>\n",
       "      <td>0.094996</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-30</th>\n",
       "      <td>0.008144</td>\n",
       "      <td>0.004305</td>\n",
       "      <td>0.014320</td>\n",
       "      <td>0.043201</td>\n",
       "      <td>0.001602</td>\n",
       "      <td>-0.011967</td>\n",
       "      <td>0.004125</td>\n",
       "      <td>0.002910</td>\n",
       "      <td>0.016998</td>\n",
       "      <td>0.001745</td>\n",
       "      <td>-0.006399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>0.064299</td>\n",
       "      <td>0.001241</td>\n",
       "      <td>-0.009634</td>\n",
       "      <td>-0.001426</td>\n",
       "      <td>0.002722</td>\n",
       "      <td>-0.002935</td>\n",
       "      <td>-0.022196</td>\n",
       "      <td>-0.021325</td>\n",
       "      <td>-0.036926</td>\n",
       "      <td>-0.050939</td>\n",
       "      <td>-0.115238</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>320 rows × 11 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 50
  },
  {
   "cell_type": "code",
   "id": "06827807",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:08:51.255472Z",
     "start_time": "2025-11-18T06:08:51.230203Z"
    }
   },
   "source": [
    "model = smf.ols('My_portfolio ~ 1',\n",
    "                 data=portfolio_beta['1997-01':'2024-12']).fit(\n",
    "                     cov_type='HAC', cov_kwds={'maxlags': 6})\n",
    "print(model.summary())"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:           My_portfolio   R-squared:                       0.000\n",
      "Model:                            OLS   Adj. R-squared:                  0.000\n",
      "Method:                 Least Squares   F-statistic:                       nan\n",
      "Date:                Tue, 18 Nov 2025   Prob (F-statistic):                nan\n",
      "Time:                        14:08:51   Log-Likelihood:                 447.13\n",
      "No. Observations:                 320   AIC:                            -892.3\n",
      "Df Residuals:                     319   BIC:                            -888.5\n",
      "Df Model:                           0                                         \n",
      "Covariance Type:                  HAC                                         \n",
      "==============================================================================\n",
      "                 coef    std err          z      P>|z|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "Intercept     -0.0062      0.003     -2.249      0.025      -0.012      -0.001\n",
      "==============================================================================\n",
      "Omnibus:                       32.065   Durbin-Watson:                   2.100\n",
      "Prob(Omnibus):                  0.000   Jarque-Bera (JB):               70.195\n",
      "Skew:                           0.516   Prob(JB):                     5.72e-16\n",
      "Kurtosis:                       5.049   Cond. No.                         1.00\n",
      "==============================================================================\n",
      "\n",
      "Notes:\n",
      "[1] Standard Errors are heteroscedasticity and autocorrelation robust (HAC) using 6 lags and without small sample correction\n"
     ]
    }
   ],
   "execution_count": 52
  },
  {
   "cell_type": "code",
   "id": "a043ed6b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:08:53.299714Z",
     "start_time": "2025-11-18T06:08:53.013478Z"
    }
   },
   "source": [
    "cross_beta2 = cross_beta.copy()\n",
    "cross_beta2 = cross_beta2.set_index(['Stkcd', 'month']) # 设置multi-index\n",
    "cross_beta2 = cross_beta2.dropna(subset=['next_ret','beta'])\n",
    "cross_beta2 = cross_beta2[(cross_beta2.index.get_level_values('month') >= '2000-01-31') & \n",
    "                          (cross_beta2.index.get_level_values('month') <= '2024-12-31')]\n",
    "cross_beta2"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                    Trdmnt  Opndt  Mopnprc  Clsdt  Mclsprc     Mnshrtrd  \\\n",
       "Stkcd  month                                                              \n",
       "000001 2000-01-31  2000-01    4.0    17.50   28.0    18.53  178385288.0   \n",
       "       2000-02-29  2000-02   14.0    19.20   29.0    18.32  220816880.0   \n",
       "       2000-03-31  2000-03    1.0    18.40   31.0    18.37  362256029.0   \n",
       "       2000-04-30  2000-04    3.0    18.38   28.0    19.05  208567616.0   \n",
       "       2000-05-31  2000-05    8.0    19.12   31.0    18.00   98004336.0   \n",
       "...                    ...    ...      ...    ...      ...          ...   \n",
       "605500 2024-07-31  2024-07    1.0     6.16   31.0     6.40   32852013.0   \n",
       "       2024-08-31  2024-08    1.0     6.40   30.0     6.18   24283030.0   \n",
       "       2024-09-30  2024-09    2.0     6.17   30.0     7.13   27875613.0   \n",
       "       2024-10-31  2024-10    8.0     7.83   31.0     7.05   49883893.0   \n",
       "       2024-11-30  2024-11    1.0     7.06   29.0     8.03   66866137.0   \n",
       "\n",
       "                       Mnvaltrd  floatingvalue    totalvalue  Ndaytrd  \\\n",
       "Stkcd  month                                                            \n",
       "000001 2000-01-31  3.299056e+09   1.984382e+10  2.875573e+10     19.0   \n",
       "       2000-02-29  4.175318e+09   1.961893e+10  2.842984e+10     12.0   \n",
       "       2000-03-31  6.859299e+09   1.967248e+10  2.850743e+10     23.0   \n",
       "       2000-04-30  3.978433e+09   2.040069e+10  2.956269e+10     20.0   \n",
       "       2000-05-31  1.779608e+09   1.927624e+10  2.793325e+10     18.0   \n",
       "...                         ...            ...           ...      ...   \n",
       "605500 2024-07-31  2.035688e+08   2.652160e+09  2.652160e+09     23.0   \n",
       "       2024-08-31  1.500448e+08   2.560992e+09  2.560992e+09     22.0   \n",
       "       2024-09-30  1.777782e+08   2.954672e+09  2.954672e+09     19.0   \n",
       "       2024-10-31  3.539763e+08   2.921520e+09  2.921520e+09     18.0   \n",
       "       2024-11-30  5.037812e+08   3.327632e+09  3.327632e+09     21.0   \n",
       "\n",
       "                     Return    Mretnd  Markettype    Capchgdt  Ahshrtrd_M  \\\n",
       "Stkcd  month                                                                \n",
       "000001 2000-01-31  0.061891  0.061891         4.0  1997-08-25         NaN   \n",
       "       2000-02-29 -0.011333 -0.011333         4.0  1997-08-25         NaN   \n",
       "       2000-03-31  0.002729  0.002729         4.0  1997-08-25         NaN   \n",
       "       2000-04-30  0.037017  0.037017         4.0  1997-08-25         NaN   \n",
       "       2000-05-31 -0.055118 -0.055118         4.0  1997-08-25         NaN   \n",
       "...                     ...       ...         ...         ...         ...   \n",
       "605500 2024-07-31  0.067517  0.065041         1.0  2024-06-24         0.0   \n",
       "       2024-08-31 -0.034375 -0.034375         1.0  2024-06-24         0.0   \n",
       "       2024-09-30  0.153722  0.153722         1.0  2024-06-24         0.0   \n",
       "       2024-10-31 -0.011220 -0.011220         1.0  2024-06-24         0.0   \n",
       "       2024-11-30  0.139007  0.139007         1.0  2024-06-24         0.0   \n",
       "\n",
       "                   Ahvaltrd_M   rfmonth  list_month       ret  next_ret  \\\n",
       "Stkcd  month                                                              \n",
       "000001 2000-01-31         NaN  0.001856       106.0  0.060035 -0.013189   \n",
       "       2000-02-29         NaN  0.001856       107.0 -0.013189  0.000873   \n",
       "       2000-03-31         NaN  0.001856       108.0  0.000873  0.035161   \n",
       "       2000-04-30         NaN  0.001856       109.0  0.035161 -0.056974   \n",
       "       2000-05-31         NaN  0.001856       110.0 -0.056974  0.005366   \n",
       "...                       ...       ...         ...       ...       ...   \n",
       "605500 2024-07-31         0.0  0.001184        44.0  0.066333 -0.035493   \n",
       "       2024-08-31         0.0  0.001118        45.0 -0.035493  0.152604   \n",
       "       2024-09-30         0.0  0.001118        46.0  0.152604 -0.012338   \n",
       "       2024-10-31         0.0  0.001118        47.0 -0.012338  0.137889   \n",
       "       2024-11-30         0.0  0.001118        48.0  0.137889 -0.134368   \n",
       "\n",
       "                   Cumsum_tradingday       MKT      beta  data_count  \n",
       "Stkcd  month                                                          \n",
       "000001 2000-01-31              236.0  0.158982  1.200583        61.0  \n",
       "       2000-02-29              241.0  0.120168  1.204760        61.0  \n",
       "       2000-03-31              241.0  0.054070  1.202779        61.0  \n",
       "       2000-04-30              239.0  0.011158  1.224677        61.0  \n",
       "       2000-05-31              238.0  0.025835  1.228584        61.0  \n",
       "...                              ...       ...       ...         ...  \n",
       "605500 2024-07-31              243.0 -0.000624  0.796606        43.0  \n",
       "       2024-08-31              242.0 -0.032586  0.798576        44.0  \n",
       "       2024-09-30              241.0  0.194874  0.800304        45.0  \n",
       "       2024-10-31              242.0 -0.004527  0.800487        46.0  \n",
       "       2024-11-30              241.0  0.008606  0.809865        47.0  \n",
       "\n",
       "[490287 rows x 24 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Trdmnt</th>\n",
       "      <th>Opndt</th>\n",
       "      <th>Mopnprc</th>\n",
       "      <th>Clsdt</th>\n",
       "      <th>Mclsprc</th>\n",
       "      <th>Mnshrtrd</th>\n",
       "      <th>Mnvaltrd</th>\n",
       "      <th>floatingvalue</th>\n",
       "      <th>totalvalue</th>\n",
       "      <th>Ndaytrd</th>\n",
       "      <th>Return</th>\n",
       "      <th>Mretnd</th>\n",
       "      <th>Markettype</th>\n",
       "      <th>Capchgdt</th>\n",
       "      <th>Ahshrtrd_M</th>\n",
       "      <th>Ahvaltrd_M</th>\n",
       "      <th>rfmonth</th>\n",
       "      <th>list_month</th>\n",
       "      <th>ret</th>\n",
       "      <th>next_ret</th>\n",
       "      <th>Cumsum_tradingday</th>\n",
       "      <th>MKT</th>\n",
       "      <th>beta</th>\n",
       "      <th>data_count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Stkcd</th>\n",
       "      <th>month</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">000001</th>\n",
       "      <th>2000-01-31</th>\n",
       "      <td>2000-01</td>\n",
       "      <td>4.0</td>\n",
       "      <td>17.50</td>\n",
       "      <td>28.0</td>\n",
       "      <td>18.53</td>\n",
       "      <td>178385288.0</td>\n",
       "      <td>3.299056e+09</td>\n",
       "      <td>1.984382e+10</td>\n",
       "      <td>2.875573e+10</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0.061891</td>\n",
       "      <td>0.061891</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>106.0</td>\n",
       "      <td>0.060035</td>\n",
       "      <td>-0.013189</td>\n",
       "      <td>236.0</td>\n",
       "      <td>0.158982</td>\n",
       "      <td>1.200583</td>\n",
       "      <td>61.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-02-29</th>\n",
       "      <td>2000-02</td>\n",
       "      <td>14.0</td>\n",
       "      <td>19.20</td>\n",
       "      <td>29.0</td>\n",
       "      <td>18.32</td>\n",
       "      <td>220816880.0</td>\n",
       "      <td>4.175318e+09</td>\n",
       "      <td>1.961893e+10</td>\n",
       "      <td>2.842984e+10</td>\n",
       "      <td>12.0</td>\n",
       "      <td>-0.011333</td>\n",
       "      <td>-0.011333</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>107.0</td>\n",
       "      <td>-0.013189</td>\n",
       "      <td>0.000873</td>\n",
       "      <td>241.0</td>\n",
       "      <td>0.120168</td>\n",
       "      <td>1.204760</td>\n",
       "      <td>61.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-03-31</th>\n",
       "      <td>2000-03</td>\n",
       "      <td>1.0</td>\n",
       "      <td>18.40</td>\n",
       "      <td>31.0</td>\n",
       "      <td>18.37</td>\n",
       "      <td>362256029.0</td>\n",
       "      <td>6.859299e+09</td>\n",
       "      <td>1.967248e+10</td>\n",
       "      <td>2.850743e+10</td>\n",
       "      <td>23.0</td>\n",
       "      <td>0.002729</td>\n",
       "      <td>0.002729</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>108.0</td>\n",
       "      <td>0.000873</td>\n",
       "      <td>0.035161</td>\n",
       "      <td>241.0</td>\n",
       "      <td>0.054070</td>\n",
       "      <td>1.202779</td>\n",
       "      <td>61.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-04-30</th>\n",
       "      <td>2000-04</td>\n",
       "      <td>3.0</td>\n",
       "      <td>18.38</td>\n",
       "      <td>28.0</td>\n",
       "      <td>19.05</td>\n",
       "      <td>208567616.0</td>\n",
       "      <td>3.978433e+09</td>\n",
       "      <td>2.040069e+10</td>\n",
       "      <td>2.956269e+10</td>\n",
       "      <td>20.0</td>\n",
       "      <td>0.037017</td>\n",
       "      <td>0.037017</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>109.0</td>\n",
       "      <td>0.035161</td>\n",
       "      <td>-0.056974</td>\n",
       "      <td>239.0</td>\n",
       "      <td>0.011158</td>\n",
       "      <td>1.224677</td>\n",
       "      <td>61.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-05-31</th>\n",
       "      <td>2000-05</td>\n",
       "      <td>8.0</td>\n",
       "      <td>19.12</td>\n",
       "      <td>31.0</td>\n",
       "      <td>18.00</td>\n",
       "      <td>98004336.0</td>\n",
       "      <td>1.779608e+09</td>\n",
       "      <td>1.927624e+10</td>\n",
       "      <td>2.793325e+10</td>\n",
       "      <td>18.0</td>\n",
       "      <td>-0.055118</td>\n",
       "      <td>-0.055118</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1997-08-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.001856</td>\n",
       "      <td>110.0</td>\n",
       "      <td>-0.056974</td>\n",
       "      <td>0.005366</td>\n",
       "      <td>238.0</td>\n",
       "      <td>0.025835</td>\n",
       "      <td>1.228584</td>\n",
       "      <td>61.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">605500</th>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2024-07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6.16</td>\n",
       "      <td>31.0</td>\n",
       "      <td>6.40</td>\n",
       "      <td>32852013.0</td>\n",
       "      <td>2.035688e+08</td>\n",
       "      <td>2.652160e+09</td>\n",
       "      <td>2.652160e+09</td>\n",
       "      <td>23.0</td>\n",
       "      <td>0.067517</td>\n",
       "      <td>0.065041</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-06-24</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.001184</td>\n",
       "      <td>44.0</td>\n",
       "      <td>0.066333</td>\n",
       "      <td>-0.035493</td>\n",
       "      <td>243.0</td>\n",
       "      <td>-0.000624</td>\n",
       "      <td>0.796606</td>\n",
       "      <td>43.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-08-31</th>\n",
       "      <td>2024-08</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6.40</td>\n",
       "      <td>30.0</td>\n",
       "      <td>6.18</td>\n",
       "      <td>24283030.0</td>\n",
       "      <td>1.500448e+08</td>\n",
       "      <td>2.560992e+09</td>\n",
       "      <td>2.560992e+09</td>\n",
       "      <td>22.0</td>\n",
       "      <td>-0.034375</td>\n",
       "      <td>-0.034375</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-06-24</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>45.0</td>\n",
       "      <td>-0.035493</td>\n",
       "      <td>0.152604</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.032586</td>\n",
       "      <td>0.798576</td>\n",
       "      <td>44.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-30</th>\n",
       "      <td>2024-09</td>\n",
       "      <td>2.0</td>\n",
       "      <td>6.17</td>\n",
       "      <td>30.0</td>\n",
       "      <td>7.13</td>\n",
       "      <td>27875613.0</td>\n",
       "      <td>1.777782e+08</td>\n",
       "      <td>2.954672e+09</td>\n",
       "      <td>2.954672e+09</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0.153722</td>\n",
       "      <td>0.153722</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-06-24</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>46.0</td>\n",
       "      <td>0.152604</td>\n",
       "      <td>-0.012338</td>\n",
       "      <td>241.0</td>\n",
       "      <td>0.194874</td>\n",
       "      <td>0.800304</td>\n",
       "      <td>45.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-10-31</th>\n",
       "      <td>2024-10</td>\n",
       "      <td>8.0</td>\n",
       "      <td>7.83</td>\n",
       "      <td>31.0</td>\n",
       "      <td>7.05</td>\n",
       "      <td>49883893.0</td>\n",
       "      <td>3.539763e+08</td>\n",
       "      <td>2.921520e+09</td>\n",
       "      <td>2.921520e+09</td>\n",
       "      <td>18.0</td>\n",
       "      <td>-0.011220</td>\n",
       "      <td>-0.011220</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-06-24</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>47.0</td>\n",
       "      <td>-0.012338</td>\n",
       "      <td>0.137889</td>\n",
       "      <td>242.0</td>\n",
       "      <td>-0.004527</td>\n",
       "      <td>0.800487</td>\n",
       "      <td>46.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-30</th>\n",
       "      <td>2024-11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>7.06</td>\n",
       "      <td>29.0</td>\n",
       "      <td>8.03</td>\n",
       "      <td>66866137.0</td>\n",
       "      <td>5.037812e+08</td>\n",
       "      <td>3.327632e+09</td>\n",
       "      <td>3.327632e+09</td>\n",
       "      <td>21.0</td>\n",
       "      <td>0.139007</td>\n",
       "      <td>0.139007</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2024-06-24</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.001118</td>\n",
       "      <td>48.0</td>\n",
       "      <td>0.137889</td>\n",
       "      <td>-0.134368</td>\n",
       "      <td>241.0</td>\n",
       "      <td>0.008606</td>\n",
       "      <td>0.809865</td>\n",
       "      <td>47.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>490287 rows × 24 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 53
  },
  {
   "cell_type": "code",
   "id": "8d574786",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:08:57.601098Z",
     "start_time": "2025-11-18T06:08:55.529902Z"
    }
   },
   "source": [
    "from linearmodels import FamaMacBeth\n",
    "model = FamaMacBeth.from_formula('next_ret ~ 1 + beta', data=cross_beta2.dropna(subset=['next_ret','beta']))\n",
    "## 一般fm回归结果展示的是Newey-West调整后的t值，.fit()中做如下设置\n",
    "## 其中`bandwidth`是Newey-West滞后阶数，选取方式为lag = 4(T/100) ^ (2/9)\n",
    "## 若不需要Newey-West调整则去掉括号内所有设置。\n",
    "# choose bandwidth auto\n",
    "res = model.fit(cov_type= 'kernel',debiased = False,bandwidth=6)\n",
    "print(res.summary)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            FamaMacBeth Estimation Summary                           \n",
      "=====================================================================================\n",
      "Dep. Variable:                    next_ret   R-squared:                     1.449e-05\n",
      "Estimator:                     FamaMacBeth   R-squared (Between):             -0.0172\n",
      "No. Observations:                   490287   R-squared (Within):               0.0001\n",
      "Date:                     Tue, Nov 18 2025   R-squared (Overall):           1.449e-05\n",
      "Time:                             14:08:57   Log-likelihood                 2.758e+05\n",
      "Cov. Estimator:    Fama-MacBeth Kernel Cov                                           \n",
      "                                             F-statistic:                      7.1056\n",
      "Entities:                             3236   P-value                           0.0077\n",
      "Avg Obs:                            151.51   Distribution:                F(1,490285)\n",
      "Min Obs:                            1.0000                                           \n",
      "Max Obs:                            299.00   F-statistic (robust):             4.2674\n",
      "                                             P-value                           0.0388\n",
      "Time periods:                          299   Distribution:                    chi2(1)\n",
      "Avg Obs:                            1639.8                                           \n",
      "Min Obs:                            426.00                                           \n",
      "Max Obs:                            2978.0                                           \n",
      "                                                                                     \n",
      "                             Parameter Estimates                              \n",
      "==============================================================================\n",
      "            Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "------------------------------------------------------------------------------\n",
      "Intercept      0.0137     0.0058     2.3460     0.0190      0.0022      0.0251\n",
      "beta          -0.0035     0.0017    -2.0658     0.0388     -0.0068     -0.0002\n",
      "==============================================================================\n"
     ]
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:09:23.993572Z",
     "start_time": "2025-11-18T06:09:23.373508Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import statsmodels.api as sm\n",
    "\n",
    "def Fama_MacBeth(data, yvar, xvars):\n",
    "    Y = data[yvar]\n",
    "    X = data[xvars]\n",
    "    X['intercept'] = 1.\n",
    "    result = sm.OLS(Y, X).fit()\n",
    "    return result.params\n",
    "\n",
    "coef = cross_beta2.dropna(subset=['next_ret','beta']).groupby('month').apply(Fama_MacBeth, 'next_ret', ['beta'])\n",
    "coef"
   ],
   "id": "4e6151a840bc664c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                beta  intercept\n",
       "month                          \n",
       "2000-01-31  0.025960   0.124545\n",
       "2000-02-29 -0.067173   0.168086\n",
       "2000-03-31 -0.023504   0.041805\n",
       "2000-04-30 -0.007965   0.044455\n",
       "2000-05-31 -0.022458   0.044424\n",
       "...              ...        ...\n",
       "2024-07-31 -0.011379  -0.021878\n",
       "2024-08-31  0.023007   0.184893\n",
       "2024-09-30  0.041929   0.020726\n",
       "2024-10-31 -0.023423   0.070724\n",
       "2024-11-30 -0.027961  -0.005796\n",
       "\n",
       "[299 rows x 2 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>beta</th>\n",
       "      <th>intercept</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>month</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-31</th>\n",
       "      <td>0.025960</td>\n",
       "      <td>0.124545</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-02-29</th>\n",
       "      <td>-0.067173</td>\n",
       "      <td>0.168086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-03-31</th>\n",
       "      <td>-0.023504</td>\n",
       "      <td>0.041805</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-04-30</th>\n",
       "      <td>-0.007965</td>\n",
       "      <td>0.044455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-05-31</th>\n",
       "      <td>-0.022458</td>\n",
       "      <td>0.044424</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>-0.011379</td>\n",
       "      <td>-0.021878</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-08-31</th>\n",
       "      <td>0.023007</td>\n",
       "      <td>0.184893</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-30</th>\n",
       "      <td>0.041929</td>\n",
       "      <td>0.020726</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-10-31</th>\n",
       "      <td>-0.023423</td>\n",
       "      <td>0.070724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-30</th>\n",
       "      <td>-0.027961</td>\n",
       "      <td>-0.005796</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>299 rows × 2 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 55
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:09:36.441559Z",
     "start_time": "2025-11-18T06:09:36.425551Z"
    }
   },
   "cell_type": "code",
   "source": [
    "model_alpha = smf.ols('intercept ~ 1',\n",
    "                 data=coef['2000-01':'2024-11']).fit(\n",
    "                     cov_type='HAC', cov_kwds={'maxlags': 6})\n",
    "print(model_alpha.summary())"
   ],
   "id": "a348207d42bdb93d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:              intercept   R-squared:                      -0.000\n",
      "Model:                            OLS   Adj. R-squared:                 -0.000\n",
      "Method:                 Least Squares   F-statistic:                       nan\n",
      "Date:                Tue, 18 Nov 2025   Prob (F-statistic):                nan\n",
      "Time:                        14:09:36   Log-Likelihood:                 341.82\n",
      "No. Observations:                 299   AIC:                            -681.6\n",
      "Df Residuals:                     298   BIC:                            -677.9\n",
      "Df Model:                           0                                         \n",
      "Covariance Type:                  HAC                                         \n",
      "==============================================================================\n",
      "                 coef    std err          z      P>|z|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "Intercept      0.0137      0.006      2.346      0.019       0.002       0.025\n",
      "==============================================================================\n",
      "Omnibus:                       17.444   Durbin-Watson:                   1.611\n",
      "Prob(Omnibus):                  0.000   Jarque-Bera (JB):               33.028\n",
      "Skew:                           0.316   Prob(JB):                     6.73e-08\n",
      "Kurtosis:                       4.501   Cond. No.                         1.00\n",
      "==============================================================================\n",
      "\n",
      "Notes:\n",
      "[1] Standard Errors are heteroscedasticity and autocorrelation robust (HAC) using 6 lags and without small sample correction\n"
     ]
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-18T06:09:47.553928Z",
     "start_time": "2025-11-18T06:09:47.535632Z"
    }
   },
   "cell_type": "code",
   "source": [
    "model_beta = smf.ols('beta ~ 1',\n",
    "                 data=coef['1994-12':'2024-11']).fit(\n",
    "                     cov_type='HAC', cov_kwds={'maxlags': 6})\n",
    "print(model_beta.summary())"
   ],
   "id": "57e036ae52be1b35",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:                   beta   R-squared:                       0.000\n",
      "Model:                            OLS   Adj. R-squared:                  0.000\n",
      "Method:                 Least Squares   F-statistic:                       nan\n",
      "Date:                Tue, 18 Nov 2025   Prob (F-statistic):                nan\n",
      "Time:                        14:09:47   Log-Likelihood:                 593.30\n",
      "No. Observations:                 299   AIC:                            -1185.\n",
      "Df Residuals:                     298   BIC:                            -1181.\n",
      "Df Model:                           0                                         \n",
      "Covariance Type:                  HAC                                         \n",
      "==============================================================================\n",
      "                 coef    std err          z      P>|z|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "Intercept     -0.0035      0.002     -2.066      0.039      -0.007      -0.000\n",
      "==============================================================================\n",
      "Omnibus:                       39.652   Durbin-Watson:                   2.125\n",
      "Prob(Omnibus):                  0.000   Jarque-Bera (JB):              110.888\n",
      "Skew:                           0.585   Prob(JB):                     8.34e-25\n",
      "Kurtosis:                       5.744   Cond. No.                         1.00\n",
      "==============================================================================\n",
      "\n",
      "Notes:\n",
      "[1] Standard Errors are heteroscedasticity and autocorrelation robust (HAC) using 6 lags and without small sample correction\n"
     ]
    }
   ],
   "execution_count": 57
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
